{"id":329506,"date":"2023-09-11T14:00:00","date_gmt":"2023-09-11T14:00:00","guid":{"rendered":"https:\/\/realpython.com\/python3-object-oriented-programming\/"},"modified":"2023-09-11T14:00:00","modified_gmt":"2023-09-11T14:00:00","slug":"object-oriented-programming-oop-in-python-3","status":"publish","type":"post","link":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/2023\/09\/11\/object-oriented-programming-oop-in-python-3\/","title":{"rendered":"Object-Oriented Programming (OOP) in Python 3"},"content":{"rendered":"<p class=\"syndicated-attribution\"><meta name= \\\"keywords \\\" content= \\\"\u96fb\u5b50\u8a08\u7b97\u6a5f, \u6559\u80b2, IT \u96fb\u8166\u73ed,\u96fb\u8166\u88dc\u7fd2\uff0c \u96fb\u8166\u73ed\uff0c \u5bb6\u6559\uff0c \u79c1\u4eba\u8001\u5e2b\uff0c \u8cc7\u8a0a\u6280\u8853\uff0c \u7a0b\u5e8f\u8a2d\u8a08\uff0c \u96fb\u5b50\u8a08\u7b97\u6a5f\uff0c \u904a\u6232\uff0c \u860b\u679c\uff0c \u96fb\u5f71\uff0c \u8a08\u7b97\u6a5f\uff0c\u7de8\u78bc\uff0c Java\uff0c C\/C++\uff0c JavaScript\uff0c PHP\uff0c HTML\uff0c CSS\uff0c MySQL\uff0c mobile\uff0c Android\uff0c \u52d5\u6f2b\uff0c Python\uff0c teacher\uff0c \u88dc\u7fd2\uff0c \u96fb\u8166\u88dc\u7fd2 \u8cc7\u8a0a, \u7535\u5b50\u8ba1\u7b97\u673a, IT ,Game, apple, movie, Computer,student,Java,\u6559\u80b2, ,\u5b66\u751f, \u5b66\u4e60, learn, \u6559\u5b66,  Android, apple,anime, animation, \u4fe1\u606f\u6280\u672f, \u7a0b\u5e8f\u8bbe\u8ba1, \u79fb\u52a8\u7535\u8bdd, \u8cc7\u8a0a\u79d1\u6280,Game, Jeu, Juego,Call Of Duty ,\u4f7f\u547d\u53ec\u559a , \u6e38\u620f, \u7535\u5b50\u6e38\u620f,, \u591a\u4eba\u7535\u5b50\u6e38\u620f, \u7f51\u7edc\u6e38\u620f\uff0conline\uff0conline game, \u624b\u673a\u6e38\u620f, mobile \\\"><\/p>\n<div>\n<p><strong>Object-oriented programming (OOP)<\/strong> is a method of structuring a program by bundling related properties and behaviors into individual <strong>objects<\/strong>. In this tutorial, you\u2019ll learn the basics of object-oriented programming in Python.<\/p>\n<p>Conceptually, objects are like the components of a system. Think of a program as a factory assembly line of sorts. At each step of the assembly line, a system component processes some material, ultimately transforming raw material into a finished product.<\/p>\n<p>An object contains data, like the raw or preprocessed materials at each step on an assembly line. In addition, the object contains behavior, like the action that each assembly line component performs.<\/p>\n<p><strong>In this tutorial, you\u2019ll learn how to:<\/strong><\/p>\n<ul>\n<li>Define a <strong>class<\/strong>, which is like a blueprint for creating an object<\/li>\n<li>Use classes to <strong>create new objects<\/strong><\/li>\n<li>Model systems with <strong>class inheritance<\/strong><\/li>\n<\/ul>\n<div class=\"alert alert-primary\" role=\"alert\">\n<p><strong>Note:<\/strong> This tutorial is adapted from the chapter \u201cObject-Oriented Programming (OOP)\u201d in <a href=\"https:\/\/realpython.com\/products\/python-basics-book\/\"><em>Python Basics: A Practical Introduction to Python 3<\/em><\/a>.<\/p>\n<p>The book uses Python\u2019s built-in <a href=\"https:\/\/realpython.com\/python-idle\/\">IDLE<\/a> editor to create and edit Python files and interact with the Python shell, so you\u2019ll see occasional references to IDLE throughout this tutorial. If you don\u2019t use IDLE, you can <a href=\"https:\/\/realpython.com\/run-python-scripts\/\">run the example code<\/a> from the editor and environment of your choice.<\/p>\n<\/div>\n<div class=\"alert alert-warning\" role=\"alert\">\n<p><strong markdown=\"1\">Get Your Code:<\/strong> <a href=\"https:\/\/realpython.com\/bonus\/object-oriented-programming-oop-in-python-3-code\/\" class=\"alert-link\" data-toggle=\"modal\" data- data-focus=\"false\" markdown=\"1\">Click here to download the free sample code<\/a> that shows you how to do object-oriented programming with classes in Python 3.<\/p>\n<\/div>\n<h2 id=\"what-is-object-oriented-programming-in-python\">What Is Object-Oriented Programming in Python?<a class=\"headerlink\" href=\"https:\/\/realpython.com\/python3-object-oriented-programming\/#what-is-object-oriented-programming-in-python\" title=\"Permanent link\"><\/a><\/h2>\n<p>Object-oriented programming is a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Programming_paradigm\">programming paradigm<\/a> that provides a means of structuring programs so that properties and behaviors are bundled into individual <strong>objects<\/strong>.<\/p>\n<p>For example, an object could represent a person with <strong>properties<\/strong> like a name, age, and address and <strong>behaviors<\/strong> such as walking, talking, breathing, and running. Or it could represent an <a href=\"https:\/\/realpython.com\/python-send-email\/\">email<\/a> with properties like a recipient list, subject, and body and behaviors like adding attachments and sending. <\/p>\n<p>Put another way, object-oriented programming is an approach for modeling concrete, real-world things, like cars, as well as relations between things, like companies and employees or students and teachers. OOP models real-world entities as software objects that have some data associated with them and can perform certain operations.<\/p>\n<div class=\"alert alert-primary\" role=\"alert\">\n<p><strong>Note:<\/strong> You can also check out the <a href=\"https:\/\/realpython.com\/courses\/python-basics-oop\/\">Python Basics: Object-Oriented Programming<\/a> video course to reinforce the skills that you\u2019ll develop in this section of the tutorial.<\/p>\n<\/div>\n<p>The key takeaway is that objects are at the center of object-oriented programming in Python. In other programming paradigms, objects only represent the data. In OOP, they additionally inform the overall structure of the program.<\/p>\n<h2 id=\"how-do-you-define-a-class-in-python\">How Do You Define a Class in Python?<a class=\"headerlink\" href=\"https:\/\/realpython.com\/python3-object-oriented-programming\/#how-do-you-define-a-class-in-python\" title=\"Permanent link\"><\/a><\/h2>\n<p>In Python, you define a class by using the <code>class<\/code> keyword followed by a name and a colon. Then you use <code>.__init__()<\/code> to declare which attributes each instance of the class should have:<\/p>\n<div class=\"highlight python\">\n<pre><span><\/span><code><span class=\"k\">class<\/span> <span class=\"nc\">Employee<\/span><span class=\"p\">:<\/span>\n    <span class=\"k\">def<\/span> <span class=\"fm\">__init__<\/span><span class=\"p\">(<\/span><span class=\"bp\">self<\/span><span class=\"p\">,<\/span> <span class=\"n\">name<\/span><span class=\"p\">,<\/span> <span class=\"n\">age<\/span><span class=\"p\">):<\/span>\n        <span class=\"bp\">self<\/span><span class=\"o\">.<\/span><span class=\"n\">name<\/span> <span class=\"o\">=<\/span>  <span class=\"n\">name<\/span>\n        <span class=\"bp\">self<\/span><span class=\"o\">.<\/span><span class=\"n\">age<\/span> <span class=\"o\">=<\/span> <span class=\"n\">age<\/span>\n<\/code><\/pre>\n<\/div>\n<p>But what does all of that mean? And why do you even need classes in the first place? Take a step back and consider using built-in, primitive <a href=\"https:\/\/realpython.com\/courses\/python-data-types\/\">data structures<\/a> as an alternative.<\/p>\n<p>Primitive data structures\u2014like <a href=\"https:\/\/realpython.com\/python-numbers\/\">numbers<\/a>, <a href=\"https:\/\/realpython.com\/python-strings\/\">strings<\/a>, and <a href=\"https:\/\/realpython.com\/python-list\/\">lists<\/a>\u2014are designed to represent straightforward pieces of information, such as the cost of an apple, the name of a poem, or your favorite colors, respectively. What if you want to represent something more complex?<\/p>\n<p>For example, you might want to track employees in an organization. You need to store some basic information about each employee, such as their name, age, position, and the year they started working.<\/p>\n<p>One way to do this is to represent each employee as a <a href=\"https:\/\/realpython.com\/python-list\/\">list<\/a>:<\/p>\n<div class=\"highlight python\">\n<pre><span><\/span><code><span class=\"n\">kirk<\/span> <span class=\"o\">=<\/span> <span class=\"p\">[<\/span><span class=\"s2\">\"James Kirk\"<\/span><span class=\"p\">,<\/span> <span class=\"mi\">34<\/span><span class=\"p\">,<\/span> <span class=\"s2\">\"Captain\"<\/span><span class=\"p\">,<\/span> <span class=\"mi\">2265<\/span><span class=\"p\">]<\/span>\n<span class=\"n\">spock<\/span> <span class=\"o\">=<\/span> <span class=\"p\">[<\/span><span class=\"s2\">\"Spock\"<\/span><span class=\"p\">,<\/span> <span class=\"mi\">35<\/span><span class=\"p\">,<\/span> <span class=\"s2\">\"Science Officer\"<\/span><span class=\"p\">,<\/span> <span class=\"mi\">2254<\/span><span class=\"p\">]<\/span>\n<span class=\"n\">mccoy<\/span> <span class=\"o\">=<\/span> <span class=\"p\">[<\/span><span class=\"s2\">\"Leonard McCoy\"<\/span><span class=\"p\">,<\/span> <span class=\"s2\">\"Chief Medical Officer\"<\/span><span class=\"p\">,<\/span> <span class=\"mi\">2266<\/span><span class=\"p\">]<\/span>\n<\/code><\/pre>\n<\/div>\n<p>There are a number of issues with this approach.<\/p>\n<p>First, it can make larger code files more difficult to manage. If you reference <code>kirk[0]<\/code> several lines away from where you declared the <code>kirk<\/code> list, will you remember that the element with <a href=\"https:\/\/realpython.com\/python-strings\/#string-indexing\">index<\/a> <code>0<\/code> is the employee\u2019s name? <\/p>\n<p>Second, it can introduce errors if employees don\u2019t have the same number of elements in their respective lists. In the <code>mccoy<\/code> list above, the age is missing, so <code>mccoy[1]<\/code> will return <code>\"Chief Medical Officer\"<\/code> instead of <a href=\"https:\/\/en.wikipedia.org\/wiki\/Leonard_McCoy\">Dr. McCoy\u2019s<\/a> age.<\/p>\n<p>A great way to make this type of code more manageable and more maintainable is to use <strong>classes<\/strong>.<\/p>\n<\/div>\n<h2><a href=\"https:\/\/realpython.com\/python3-object-oriented-programming\/?utm_source=realpython&#038;utm_medium=rss\">Read the full article at https:\/\/realpython.com\/python3-object-oriented-programming\/ \u00bb<\/a><\/h2>\n<hr \/>\n<p><em>[ Improve Your Python With ? Python Tricks ? \u2013 Get a short &amp; sweet Python Trick delivered to your inbox every couple of days. <a href=\"https:\/\/realpython.com\/python-tricks\/?utm_source=realpython&amp;utm_medium=rss&amp;utm_campaign=footer\">&gt;&gt; Click here to learn more and see examples<\/a> ]<\/em><\/p>\n\n<p class=\"syndicated-attribution\"><figure class= \\\"wp-block-image alignnone \\\"><img src= \\\"http:\/\/itteacheritfreelance.hk\/test\/wordpress\/wp-content\/uploads\/2016\/05\/logo2-2.png\\\" alt=\\\"IT\u96fb\u8166\u88dc\u7fd2 java\u88dc\u7fd2 \u70ba\u5927\u5bb6\u914d\u5c0d\u96fb\u8166\u88dc\u7fd2,IT freelance, \u79c1\u4eba\u8001\u5e2b, PHP\u88dc\u7fd2,CSS\u88dc\u7fd2,XML,Java\u88dc\u7fd2,MySQL\u88dc\u7fd2,graphic design\u88dc\u7fd2,\u4e2d\u5c0f\u5b78ICT\u88dc\u7fd2,\u4e00\u5c0d\u4e00\u79c1\u4eba\u88dc\u7fd2\u548cFreelance\u81ea\u7531\u5de5\u4f5c\u914d\u5c0d\u3002\\\"\/><figcaption>\u7acb\u523b\u8a3b\u518a\u53ca\u5831\u540d\u96fb\u8166\u88dc\u7fd2\u8ab2\u7a0b\u5427!<\/figcaption><\/figure>\r\n<\/br>Find A Teacher Form:\r\n<\/br>https:\/\/docs.google.com\/forms\/d\/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ\/viewform?edit_requested=true#responses\r\n<\/br><\/br>Email:\r\n<\/br>public1989two@gmail.com<br><br><br><br><br><br><br>\r\n<a href=www.itsec.hk style=color:#FFFFFF;>www.itsec.hk<\/a><br>\r\n<a href=\\\"www.itsec.vip\\\" style=color:#FFFFFF;>www.itsec.vip<\/a><br>\r\n<a href=\\\"www.itseceu.uk\\\" style=color:#FFFFFF;>www.itseceu.uk<\/a><br><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>In this tutorial, you&#8217;ll learn all about object-oriented programming (OOP) in Python. You&#8217;ll learn the basics of the OOP paradigm and cover concepts like classes and inheritance. You&#8217;ll also see how to instantiate an object from a class.<\/p>\n<\/div>","protected":false},"author":2055,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Object-Oriented Programming (OOP) in Python 3 - ITTeacherITFreelance.hk","description":"In this tutorial, you'll learn all about object-oriented programming (OOP) in Python. You'll learn the basics of the OOP paradigm and cover concepts like classe"},"footnotes":""},"categories":[10700],"tags":[],"_links":{"self":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329506"}],"collection":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/2055"}],"replies":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=329506"}],"version-history":[{"count":1,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329506\/revisions"}],"predecessor-version":[{"id":329507,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329506\/revisions\/329507"}],"wp:attachment":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=329506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=329506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=329506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}