Difference between revisions of "Object-oriented Python"

From PrattWiki
Jump to navigation Jump to search
(A simple guide to basic object-oriented programming in Python.)
(Properties of Instance)
 
(One intermediate revision by the same user not shown)
Line 72: Line 72:
 
==Data Encapsulation==
 
==Data Encapsulation==
  
 +
Since the variables are accessible within the function, we can write a function to encapsulate them so that we don't have to write another function to read them after creating an instance. The functions ''print_score'' is called the function of the class Student. In the following example, the code '''bart.print_score()''' executes the function of the class within the instance '''bart'''.
 +
 +
<syntaxhighlight lang=python>
 +
>>> class Student(object):
 +
...    def __init__(self, name, score):
 +
...        self.name = name
 +
...        self.score = score
 +
...    def print_score(self):
 +
...        print("%s: %s" % (self.name, self.score))
 +
...
 +
>>> bart = Student('zhang', 99)
 +
>>> bart.print_score()
 +
zhang: 99
 +
</syntaxhighlight >
  
 
=Inheritance and Polymorphism=
 
=Inheritance and Polymorphism=
  
 
=Properties of Instance=
 
=Properties of Instance=
 +
==__str__==
 +
 +
 +
==__iter__ and __next__==
 +
 +
 +
==__getitem__==
 +
 +
 +
==__getattr__==
 +
 +
 +
==__call__==
  
 
=Multiple Inheritance=
 
=Multiple Inheritance=
  
 
=Enumeration=
 
=Enumeration=

Latest revision as of 03:32, 2 October 2019

The concept of object-oriented programming originate from the natural world, which is natural to use Class and Instance. Class is an abstract concept referring to a general category of objects. For example, we can define a Class to be students, so instances of the defined Class are specific students such as Bart, Lisa, and Simpson.

The level of abstraction of object-oriented program is higher than functions, because a Class includes both data and operations of the data.

Data Encapsulation, inheritance, and Polymorphism are three fundamental aspects of object-oriented programming.

Class and Instance

A Class refers to an abstract template of codes, while a Instance is defined as specific objects based a Class. Every Instance shares the same function inherited from the Class, but data for each Instance might be different.

class Student(object):
    pass

The code above defines a Class. Right after the keyword class is the name of the class Student, which usually starts with an upper case letter. Then (object) indicates which class is the Student inherited from. If you don't have a specific class to inherit, use object since it is the root class where all class inherits.

After defining the class, we can create a instance for Student.

>>> class Student(object):
...     pass
...
>>> bart = Student() # bart is a instance of Student()
>>> bart
<__main__.Student object at 0x101be77f0>
>>> Student # Student is a class
<class '__main__.Student'>

You can bind variables to the instance as a property. For example, bind a name property to instance bart:

>>> bart.name = 'duke'
>>> bart.name
'duke'

Meanwhile, class also serves as a template to write public properties into the class, which is done by a special function __init__:

class Student(object):
    def __init__(self, name, score):
        self.name = name
        self.score = score

The first argument of the __init__ function is always self, which represents the created instance. Within the __init__ function, you can bind different properties on self because self is the instance.

With the __init__ function, you have to have corresponding arguments when creating an instance, but self is not required because Python automatically pass the self argument into the function. The following is a example

>>> class Student(object):
...    def __init__(self, name, score):
...        self.name = name
...        self.score = score
...
>>> bart = Student('duke', 99)
>>> bart.name
'duke'
>>> bart.score
99
>>> bart_test = Student('NCState')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-97f4e2f67951> in <module>()
----> 1 bart_test = Student('NCState')

TypeError: __init__() takes exactly 3 arguments (2 given)

Data Encapsulation

Since the variables are accessible within the function, we can write a function to encapsulate them so that we don't have to write another function to read them after creating an instance. The functions print_score is called the function of the class Student. In the following example, the code bart.print_score() executes the function of the class within the instance bart.

>>> class Student(object):
...     def __init__(self, name, score):
...         self.name = name
...         self.score = score
...     def print_score(self):
...         print("%s: %s" % (self.name, self.score))
...
>>> bart = Student('zhang', 99)
>>> bart.print_score()
zhang: 99

Inheritance and Polymorphism

Properties of Instance

__str__

__iter__ and __next__

__getitem__

__getattr__

__call__

Multiple Inheritance

Enumeration