{"id":2552,"date":"2025-10-08T11:15:11","date_gmt":"2025-10-08T03:15:11","guid":{"rendered":"http:\/\/blue.yn.cn\/?p=2552"},"modified":"2025-10-12T16:07:28","modified_gmt":"2025-10-12T08:07:28","slug":"python-objects","status":"publish","type":"post","link":"http:\/\/blue.yn.cn\/?p=2552","title":{"rendered":"Python Objects"},"content":{"rendered":"<p><strong>Object-oriented programming<\/strong><\/p>\n<h3>Managing larger program<\/h3>\n<p>At the beginning of this book, we came up with four basic programming patterns which we use to construct programs:<\/p>\n<ul>\n<li>\n<p>Sequential code<\/p>\n<\/li>\n<li>\n<p>Conditional cod (if statements)<\/p>\n<\/li>\n<li>\n<p>Repetitive code (loops)<\/p>\n<\/li>\n<li>\n<p>Store and reuse (functions)<\/p>\n<\/li>\n<\/ul>\n<p>In later chapters, we explored simple variables as well as collection data structures like lists, tuples, and dictionaries.<\/p>\n<p>As we build programs, we design data structures and write code to manipulate those data structures. There are many ways to write programs and by now, you probably have written some programs that are &quot;not so elegant&quot; and other programs that are &quot;more elegant&quot;. Even though your progras may be small, you are starting to see how there is a bit of art and aesthetic to writing code.<\/p>\n<p>As programs get to be millions of lines long, it becomes increasingly important to write code that is easy to understand. If you are working on a million-line program, you can never keep the entire program in your mind at the same time. We need ways to break large programs into multiple smaller pieces so that we have less to look at when solving a program, fix a bug, or add a new feature.<\/p>\n<p>In a way, object oriented programming, it is necessary to learn the concepts of object oriented programming before you can use them effectively. You should approach this chapter as a way to learn some terms and concepts and work through a few simple example to lay a foundation for future learning.<\/p>\n<p>The key outcome of this chapter is to have a basic understanding of how object are constructed and how they function and most importantly how we make use of the capabilities of objects that are provided to us by Python and Python libraries.<\/p>\n<h3>Using objects<\/h3>\n<p>As it turns out, we have been using objects all along in this book. Python provides us with many built-in objects. Here is some simple code where the first few lines should feel very simple and natural to you.<\/p>\n<pre><code class=\"language-python\">stuff = list()\nstuff.append(&#039;python&#039;)\nstuff.append(&#039;chuck&#039;)\nstuff.sort()\nprint (stuff[0])\nprint (stuff.__getitem__(0))\nprint (list.__getitem__(stuff,0))<\/code><\/pre>\n<p>Instead of focusing on what these lines accomplish, let's look at what is really happening from the point of view of object-oriented programming. Don't worry if the following paragraph don't make any sense the first time you read them because we have not yet defined all of these terms.<\/p>\n<p>The first line constructs an object of type <code>list<\/code>, the second and third lines call the <code>append()<\/code> method, the fouth line calls the <code>sort()<\/code> method, and the fifthe line retrieves the item at position 0.<\/p>\n<p>The sixth line calls the <code>__getitem__()<\/code> method in the <code>stuff<\/code> list with a parameter of zero.<\/p>\n<p><code>print (stuff.__getitem__(0))<\/code><\/p>\n<p>The seventh line is an even more verbose way of retrieving the 0th item in the list.<\/p>\n<p><code>print (list.__getitem__(stuff,0))<\/code><\/p>\n<p>In this code, we call the <code>__getitem__<\/code> method in the <code>list<\/code> class and pass the list and the item we want retrieved from the list as parameters.<\/p>\n<p>The last three lines of the program are equivalent, but it is more convenient to simply use the square brackets syntax to look up an item at a particular position in a list.<\/p>\n<p>We can take a look at the capabilities of an object by looking at the output of the <code>dir()<\/code> function:<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; stuff = list()\n>&gt;&gt; dir(stuff)\n[&#039;__add__&#039;, &#039;__class__&#039;, &#039;__contains__&#039;, &#039;__delattr__&#039;,\n&#039;__delitem__&#039;, &#039;__dir__&#039;, &#039;__doc__&#039;, &#039;__eq__&#039;,\n&#039;__format__&#039;, &#039;__ge__&#039;, &#039;__getattribute__&#039;, &#039;__getitem__&#039;,\n&#039;__gt__&#039;, &#039;__hash__&#039;, &#039;__iadd__&#039;, &#039;__imul__&#039;, &#039;__init__&#039;,\n&#039;__iter__&#039;, &#039;__le__&#039;, &#039;__len__&#039;, &#039;__lt__&#039;, &#039;__mul__&#039;,\n&#039;__ne__&#039;, &#039;__new__&#039;, &#039;__reduce__&#039;, &#039;__reduce_ex__&#039;,\n&#039;__repr__&#039;, &#039;__reversed__&#039;, &#039;__rmul__&#039;, &#039;__setattr__&#039;,\n&#039;__setitem__&#039;, &#039;__sizeof__&#039;, &#039;__str__&#039;, &#039;__subclasshook__&#039;,\n&#039;append&#039;, &#039;clear&#039;, &#039;copy&#039;, &#039;count&#039;, &#039;extend&#039;, &#039;index&#039;,\n&#039;insert&#039;, &#039;pop&#039;, &#039;remove&#039;, &#039;reverse&#039;, &#039;sort&#039;]\n>&gt;&gt;<\/code><\/pre>\n<p>The rest of this chapter will define all of the above terms so make sure to come back after you finish the chapter and re-read the above paragraphs to check your understanding.<\/p>\n<h3>Starting with programs<\/h3>\n<p>A program in its most basic form takes some input, does some processing, and produces some output. Our elevator conversion program demonstrates a very short but complete program showing all three of these steps.<\/p>\n<pre><code class=\"language-python\">usf = input(&#039;Enter the US Floor Number: &#039;)\nwf = int(usf) - 1\nprint(&#039;Non-US Floor Number is&#039;,wf)<\/code><\/pre>\n<p>If we think a bit more about this program, there is the &quot;outside world&quot; and the program. The input and output aspects are where the program interacts with the outside world. Within the program we have code and data to accomplish the task the program is designed to solve.<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2025\/10\/image-1759929684839.png\" alt=\"file\" \/><\/p>\n<p>A program<br \/>\nOne way to think about object-oriented programming is that it separates our program into multiple &quot;zones&quot;. Each zone contains some code and data (like a program) and has well defined interactions with the outside world and the other zones within the program.<\/p>\n<p>If we look back at the link extraction application where we used the BeautifulSoup library, we can see a program that is constructed by connecting different objects together to accomplish a task:<\/p>\n<pre><code class=\"language-python\"># To run this, download the BeautifulSoup zip file\n# http:\/\/www.py4e.com\/code3\/bs4.zip\n# or pip install beautifulsoup4 to ensure you have the latest version\n# and unzip it in the same directory as this file\n\nimport urllib.request, urllib.parse, urllib.error\nfrom bs4 import BeautifulSoup\nimport ssl # defauts to certicate verification and most secure protocol (now TLS)\n\n# Ignore SSL\/TLS certificate errors\nctx = ssl.create_default_context()\nctx.check_hostname = False\nctx.verify_mode = ssl.CERT_NONE\n\nurl = input(&#039;Enter - &#039;)\nhtml = urllib.request.urlopen(url, context=ctx).read()\nsoup = BeautifulSoup(html, &#039;html.parser&#039;)\n\n# Retrieve all of the anchor tags\ntags = soup(&#039;a&#039;)\nfor tag in tags:\n    print(tag.get(&#039;href&#039;, None))<\/code><\/pre>\n<p>We read the URL into a string and then pass that into <code>urllib<\/code> to retrieve the data from the web. The <code>urllib<\/code> library uses the <code>socket<\/code> library to make the actual network connection to retrieve the data. We take the string that <code>urllib<\/code> returns and hand it to BeautifulSoup for parsing. BeautifulSoup makes use of the object <code>html.parser<\/code> and returns an object. We call the <code>tags()<\/code> method on the returned object that returns a dictionary of tag object. We loop through the tags and call the <code>get()<\/code> method for each tag to print out the <code>href<\/code> attribute.<\/p>\n<p>We can draw a picture of this program and how the objects work together:<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2025\/10\/image-1759930313955.png\" alt=\"file\" \/><br \/>\nA program as Network of Objects<\/p>\n<p>The key here is not to understand perfectly how this program works but to see how we build a network of interacting objects and orchestrate the movement of information between the objects to create a program. It is also important to note that when you looked at that program several chapters back, you could fully understand what was going on in the program without even realizing that the program was &quot;orchestrating the movement of data between objects.&quot; It was just lines of code that got the job done.<\/p>\n<h3>Subdividing a problem<\/h3>\n<p>One of the advantages of the object-oriented approach is that it can hide complexity. For example, while we need to know how to use the <code>urllib<\/code> and BeautifulSoup code, we do not need to know how those libraries work internally. This allows us to focus on the part of the problem we need to solve and ignore the other parts of the program.<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2025\/10\/image-1759930922047.png\" alt=\"file\" \/><br \/>\nIgnoring Detail When Using an Object<\/p>\n<p>This ability to focus exclusively on the part of a program that we care about and ignore the rest is also helpful to the developers of the objects that we use. For example, the programmers developing BeautifulSoup do not need to know or care about how we retrieve our HTML page, what parts we want to read, or what we plan to do with the data we extract from the web page.<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2025\/10\/image-1759931179705.png\" alt=\"file\" \/><br \/>\nIgnoring Detail When Building an Object<\/p>\n<h3>Our first Python object<\/h3>\n<p>At a basic level, an object is simply some code plus data structures that are smaller than a whole program. Defining a function allows us to store a bit of code and give it a name and then later invoke that code using the name of the function.<\/p>\n<p>An object can contain a number of functions (which we call methods) as well as data that is used by those functions. We call data items that are part of the object attributes.<\/p>\n<p>We use the <code>class<\/code> keyword to define the data and code that will make up each of the objects. The class keyword inclueds the name of the class and begins an indented block of code where we include the attributes (data) and methods (code).<\/p>\n<pre><code class=\"language-python\">class PartyAnimal:\n\n   def __init__(self):\n     self.x = 0\n\n   def party(self) :\n     self.x = self.x + 1\n     print(&quot;So far&quot;,self.x)\n\nan = PartyAnimal()\nan.party()\nan.party()\nan.party()<\/code><\/pre>\n<p>Each method looks like a function, starting with the <code>def<\/code> keyword and consisting of an indented block of code.<\/p>\n<p>The first method is a specially-named method called <code>__init()__<\/code>. This method is called to do any initial setup of the data we want to store in the object. In this class we allocate the <code>x<\/code> attribute using dot notation and initialize it to zero.<\/p>\n<pre><code class=\"language-python\">self.x = 0<\/code><\/pre>\n<p>The other method name <code>party<\/code>. The mothods all have a special first parameter that we name by convention <code>self<\/code>. The first parameter gives us access to the object instance so we can set attributes and call methods using dot notation.<\/p>\n<p>Just as the <code>def<\/code> keyword does not cause function code to be executed, the <code>class<\/code> keyword does not create an object. Instead, the <code>class<\/code> keyword defines a template indicating what data and code will be contained in each object of type <code>PartyAnimal<\/code>. The class is like a cookie cutter and the objects created using the class are the cookies. You don't put frosting on the cookie cutter; you put frosting on the cookies, and you can put different frosting on each cookie.<\/p>\n<p>If we continue through this sample program, we see the first executable line of code:<\/p>\n<pre><code class=\"language-python\">an = PartyAnimal()<\/code><\/pre>\n<p>This is where we instruct Python to construct (i.e., create) an object or instance of the class <code>PartyAnimal<\/code>. It looks like a function call to the class itself. Python constructs the object with the right data and methods and returns the object which is then assigned to the variable <code>an<\/code>. In a way this is quite similar to the following line which we have been using all along:<\/p>\n<pre><code class=\"language-python\">counts = dict()<\/code><\/pre>\n<p>Here we instruct Python to construct an object using the <code>dict<\/code> template (already present in Python), return the instance of dictionary, and assign it to the variable <code>counts<\/code>.<\/p>\n<p>When the <code>PartyAnimal<\/code> class is used to construct an object, the variable <code>an<\/code> is used to poin to that object. We use <code>an<\/code> to access the code and data for that particular instance of the <code>PartyAnimal<\/code> class.<\/p>\n<p>Each PartyAnimal object\/instance contains within it a variable <code>x<\/code> and a method\/function name <code>party<\/code>. We call the <code>party<\/code> method in this line:<\/p>\n<pre><code class=\"language-python\">an.party()<\/code><\/pre>\n<p>When the <code>party<\/code> method is called, the first parameter (which we call by convention <code>self<\/code>) poits to the particular instance of the PartyAnimal object that <code>party<\/code> is called from. Within the <code>party<\/code> method, we see the line:<\/p>\n<pre><code class=\"language-python\">self.x = self.x + 1<\/code><\/pre>\n<p>This syntax using the dot operate is saying 'the x within self.' Each time <code>party()<\/code> is called, the internal <code>x<\/code> value is incremented by 1 and the value is printed out.<\/p>\n<p>The following line is another way to call the <code>party<\/code> method within the <code>an<\/code> object:<\/p>\n<pre><code class=\"language-python\">PartyAnimal.party(an)<\/code><\/pre>\n<p>In this variation, we access the code from within the calss and explicitly pass the object pointer <code>an<\/code> as the first parameter (i.e., <code>self<\/code> within the method). You can think of <code>an.party()<\/code> as shorthand for the above line.<\/p>\n<p>When the program executes, it produces the following output:<\/p>\n<pre><code class=\"language-python\">So far 1\nSo far 2\nSo far 3\nSo far 4<\/code><\/pre>\n<p>The object is constructed, and the <code>party<\/code> method is called four times, both incrementing and printing the value for <code>x<\/code> within the <code>an<\/code> object.<\/p>\n<h3>Classes as types<\/h3>\n<p>As we have seen, in Python all variables have a type. We can use the built-in <code>dir<\/code> function to examine the capabilities of a variable. We can also <code>type<\/code> and <code>dir<\/code> with the classes that we create.<\/p>\n<pre><code class=\"language-python\">class PartyAnimal:\n\n   def __init__(self):\n     self.x = 0\n\n   def party(self) :\n     self.x = self.x + 1\n     print(&quot;So far&quot;,self.x)\n\nan = PartyAnimal()\nprint (&quot;Type&quot;, type(an))\nprint (&quot;Dir &quot;, dir(an))\nprint (&quot;Type&quot;, type(an.x))\nprint (&quot;Type&quot;, type(an.party))<\/code><\/pre>\n<p>When this program executes, it produces the following output:<\/p>\n<pre><code class=\"language-python\">Type &lt;class &#039;__main__.PartyAnimal&#039;&gt;\nDir  [&#039;__class__&#039;, &#039;__delattr__&#039;, ...\n&#039;__sizeof__&#039;, &#039;__str__&#039;, &#039;__subclasshook__&#039;,\n&#039;__weakref__&#039;, &#039;party&#039;, &#039;x&#039;]\nType &lt;class &#039;int&#039;&gt;\nType &lt;class &#039;method&#039;&gt;<\/code><\/pre>\n<p>You can see that using the <code>class<\/code> keyword, we have have created a new type. From the <code>dir<\/code> output, you can see both the <code>x<\/code> integer attribute and the <code>party<\/code> memthod are availablein the object.<\/p>\n<h3>Object lifecycle<\/h3>\n<p>In the previous examples, we difine a class (template), use that class to create an instance of that class (object), and then use the instance. When the program finishes, all of the variables are discarded. Usually, we don't think much about the creation and destruction of variable, but often as our objects become more complex, we need to take some action within the object to set things up as the object is constructed and possibly clena things up as the object is discarded.<\/p>\n<p>If we want our object to be aware of these moments of construction adn destruction, we add specially named  methods to our object:<\/p>\n<pre><code class=\"language-python\">class PartyAnimal:\n\n   def __init__(self):\n     self.x = 0\n     print(&#039;I am constructed&#039;)\n\n   def party(self) :\n     self.x = self.x + 1\n     print(&#039;So far&#039;,self.x)\n\n   def __del__(self):\n     print(&#039;I am destructed&#039;, self.x)\n\nan = PartyAnimal()\nan.party()\nan.party()\nan = 42\nprint(&#039;an contains&#039;,an)<\/code><\/pre>\n<p>When this program executes, it produces the following output:<\/p>\n<pre><code class=\"language-python\">I am constructed\nSo far 1\nSo far 2\nI am destructed 2\nan contains 42<\/code><\/pre>\n<p>As Python constructs our object, it calls our <code>__init__<\/code> method to give us a chance to set up some default or initial values for the object. When Pyhthon encounters the line:<\/p>\n<pre><code class=\"language-python\">an = 42<\/code><\/pre>\n<p>I actually &quot;throws our object away&quot; so it can reuse the <code>an<\/code> variable to store the value <code>42<\/code>. Just at the moment when our <code>an<\/code> object is being &quot;destroyed&quot; our destructor code (<code>__del__<\/code>) is called. We cannot stop our variable from being destroyed, but we can do any necessary cleanup right before our object no longer exists.<\/p>\n<p>When developing objects, it is quite common to add a constructor to an object to set up initial values for the object. It is relatively rare to need a destructor for an object.<\/p>\n<h3>Multiple instances<\/h3>\n<p>So far, we have defined a class, constructed a single object, used that object, and then thrown the object away. However, the real power in object-oriented programming happens when we construct multiple instances of our class.<\/p>\n<p>When we construct multiple objects from our class, we might want to set up difference initial values for each of the objects. We can pass data to the constructors to give each object a different initial value:<\/p>\n<pre><code class=\"language-python\">class PartyAnimal:\n\n   def __init__(self, nam):\n     self.x = 0\n     self.name = nam\n     print(self.name,&#039;constructed&#039;)\n\n   def party(self) :\n     self.x = self.x + 1\n     print(self.name,&#039;party count&#039;,self.x)\n\ns = PartyAnimal(&#039;Sally&#039;)\ns.party()\nj = PartyAnimal(&#039;Jim&#039;)\n\nj.party()\ns.party()<\/code><\/pre>\n<p>The constructor has both a <code>self<\/code> parameter that points to the object instance and additional parameters that are passed into the constructor as the object is constructed:<\/p>\n<pre><code class=\"language-python\">s = PartyAnimal(&#039;Sally&#039;)<\/code><\/pre>\n<p>Within the constructor, the sencond line copies the parameter (<code>nam<\/code>) that is passed into the <code>name<\/code> attribute within the object instance.<\/p>\n<pre><code class=\"language-python\">self.name = nam<\/code><\/pre>\n<p>The output of the program shows that each of the object (<code>s<\/code> and <code>j<\/code>) contain their own independent copies of <code>x<\/code> and <code>nam<\/code>:<\/p>\n<pre><code class=\"language-python\">Sally constructed\nSally party count 1\nJim constructed\nJim party count 1\nSally party count 2<\/code><\/pre>\n<h3>Inheritance<\/h3>\n<p>Another powerful feature of object-oriented programming is the ability to crate a new class by extending an existing class. When extending a class, we call the original class the parent class and the new class the child calss.<\/p>\n<p>For this example, we move our <code>PartyAnimal<\/code> class into its file. Then, we can 'import' the <code>PartyAnimal<\/code> class in a new file and extend it, as follow:<\/p>\n<pre><code class=\"language-python\">from party import PartyAnimal\n\nclass CricketFan(PartyAnimal):\n\n   def __init__(self, nam) :\n       super().__init__(nam)\n       self.points = 0\n\n   def six(self):\n      self.points = self.points + 6\n      self.party()\n      print(self.name,&quot;points&quot;,self.points)\n\ns = PartyAnimal(&quot;Sally&quot;)\ns.party()\nj = CricketFan(&quot;Jim&quot;)\nj.party()\nj.six()\nprint(dir(j))<\/code><\/pre>\n<p>When we define the <code>ChricketFan<\/code> class, we indicate that we are extending the <code>PartyAnimal<\/code> class. This means that all of the variables (<code>x<\/code>) and methods (<code>party<\/code>) from the <code>PartyAnimal<\/code> class are inherited by the <code>CricketFan<\/code> class. For example, within the <code>six<\/code> method in the <code>CricketFan<\/code> class, we call the <code>party<\/code> method from the <code>PartyAnimal<\/code> class.<\/p>\n<pre><code class=\"language-python\">   def __init__(self, nam) :\n       super().__init__(nam)\n       self.points = 0<\/code><\/pre>\n<p>The <code>super()<\/code> syntax is telling Python to call the <code>__init__<\/code> method in the class that we are extending. <code>PartyAnimal<\/code> is the super (or parent) class and <code>CricketFan<\/code> is the sub (or child) class.<\/p>\n<p>As the program executes, we create <code>s<\/code> and <code>j<\/code> as independent instances of <code>PartyAnimal<\/code> and <code>CricketFan<\/code>. The <code>j<\/code> object has additional capabilities beyond the <code>s<\/code> object.<\/p>\n<pre><code class=\"language-python\">Sally constructed\nSally party count 1\nJim constructed\nJim party count 1\nJim party count 2\nJim points 6\n[&#039;__class__&#039;, &#039;__delattr__&#039;, ... &#039;__weakref__&#039;,\n&#039;name&#039;, &#039;party&#039;, &#039;points&#039;, &#039;six&#039;, &#039;x&#039;]<\/code><\/pre>\n<p>In the <code>dir<\/code> output for the <code>j<\/code> object (instance of the <code>CricketFan<\/code> class), we see that it has the attributes and methods of the parent class, as well as the attributes and methods that were added when the class was extended to create the <code>CricketFan<\/code> class.<\/p>\n<h3>Summary<\/h3>\n<p>This is a very quick introduction to object-oriented programming that focuses mainly on terminology and the syntax of defining and using objects. Let's quickly review the code that we looked at in the beginning of the chapter. At this point you should fully understand what is going on.<\/p>\n<pre><code class=\"language-python\">stuff = list()\nstuff.append(&#039;python&#039;)\nstuff.append(&#039;chuck&#039;)\nstuff.sort()\nprint (stuff[0])\nprint (stuff.__getitem__(0))\nprint (list.__getitem__(stuff,0))<\/code><\/pre>\n<p>The first line constructs a <code>list<\/code> object. When Python creates the <code>list<\/code> object, it calls the constructor method (named <code>__init__<\/code>) to set up the internal data attributes that will be used to store the list data. We have not passed any parameters to the constructor. When the constructor returns, we use the variable <code>stuff<\/code> to point to the returned instance of the <code>list<\/code> class.<\/p>\n<p>The second and third lines call the <code>append<\/code> method with one parameter to add a new item at the end of the list by updating the attributes wihin <code>stuff<\/code>. Then in the fourth line, we call the <code>sort<\/code> method with no parameters to sort the data within the <code>stuff<\/code> object.<\/p>\n<p>We then print out the first item in the list using the square brackets which are a shortcut to calling the <code>__getitem__<\/code> method within the <code>sduff<\/code>. This is equivalent to calling the <code>__getitem__<\/code> method in the <code>list<\/code> class and passing the <code>stuff<\/code> object as the first parameter and the position we are looking for as the second parameter.<\/p>\n<p>At the end of the program, the <code>stuff<\/code> object is disgarded but not before calling the destructed (name <code>__del__<\/code>) so that the object can clean up any loose ends as necessary.<\/p>\n<p>Those are the basics of object-oriented programming. There are many additional of details as to how to best use object-oriented approaches when developing large applications and libraries that are beyond the scope of this chapter.<\/p>\n<h3>Glossary<\/h3>\n<p><strong>attribute<\/strong><br \/>\nA variable that is part of a class.<\/p>\n<p><strong>class<\/strong><br \/>\nA template that can be used to construct an object. Defines the attributes and methods that will make up the object.<\/p>\n<p><strong>child class<\/strong><br \/>\nA new class created when a parent class is extended. The child classs inherits all of the attributes and methods of the parent class.<\/p>\n<p><strong>constructor<\/strong><br \/>\nAn optional specially named method (<code>__init__<\/code>) that is called at the moment when a class is being used to construct an object. Usually this is used to set up initial values for the object.<\/p>\n<p><strong>destructor<\/strong><br \/>\nAn optional sepcially named method (<code>__del__<\/code>) that is called at the moment just before an object is destroyed. Destructor are rarely used.<\/p>\n<p><strong>inheritance<\/strong><br \/>\nWhen we create a new class (child) by extending an existing class (parent). The child class has all the attributes and methods of the parent class plus additional attirbutes and methods defined by the child class.<\/p>\n<p><strong>method<\/strong><br \/>\na function that is contained within a class and the objects that are constructed from the class. Some object-oriented patterns use 'message' instead of 'method' to describe this concept.<\/p>\n<p><strong>object<\/strong><br \/>\nA constructed instance of a class. An object contains all of the attributes and methods that were defined by the class. Some object-oriented documentation uses the term 'instance' interchangeably with 'object'.<\/p>\n<p><strong>parent class<\/strong><br \/>\nThe class which is being extended to create a new child class. The parent class contributes all of its methods and attributes to the new child class.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Object-oriented programming Managing larger program At &#8230; &raquo; <a class=\"read-more-link\" href=\"http:\/\/blue.yn.cn\/?p=2552\">\u9605\u8bfb\u5168\u6587<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-2552","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"http:\/\/blue.yn.cn\/index.php?rest_route=\/wp\/v2\/posts\/2552","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blue.yn.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blue.yn.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blue.yn.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blue.yn.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2552"}],"version-history":[{"count":6,"href":"http:\/\/blue.yn.cn\/index.php?rest_route=\/wp\/v2\/posts\/2552\/revisions"}],"predecessor-version":[{"id":2565,"href":"http:\/\/blue.yn.cn\/index.php?rest_route=\/wp\/v2\/posts\/2552\/revisions\/2565"}],"wp:attachment":[{"href":"http:\/\/blue.yn.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blue.yn.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2552"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blue.yn.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}