basic_python/oop.rst
changeset 108 d90e054894cd
parent 88 1e5c78018aa0
equal deleted inserted replaced
107:80a8b46754f8 108:d90e054894cd
     1 Classes and Objects
     1 Classes and Objects
     2 ===================
     2 ===================
     3 
     3 
     4 In the previous sections we learnt about functions which provide certain level
     4 In the previous sections we learnt about functions which provide certain level
     5 of abstraction to our code by holding the code which performs one or more
     5 of abstraction to our code by holding the code which performs one or more
     6 specific functionalities. We were able to use this function as many times as we
     6 specific functionality. We were able to use this function as many times as we
     7 wanted. In addition to functions, Python also higher level of abstractions
     7 wanted. In addition to functions, Python also higher level of abstractions
     8 through *Classes* and *Objects*. *Objects* can be loosely defined as a
     8 through *Classes* and *Objects*. *Objects* can be loosely defined as a
     9 collection of a set of data items and a set of methods. The data items can be
     9 collection of a set of data items and a set of methods. The data items can be
    10 any valid Python variable or any Python object. Functions enclosed within a class
    10 any valid Python variable or any Python object. Functions enclosed within a class
    11 are called as *methods*. If you are thinking if methods are functions why is there
    11 are called as *methods*. If you are thinking if methods are functions why is there
    29 differences between the normal Python functions and class methods defined above.
    29 differences between the normal Python functions and class methods defined above.
    30 Each method of the class must take the same instance of the class(object) from
    30 Each method of the class must take the same instance of the class(object) from
    31 which it was called as the first argument. It is conventionally given the name,
    31 which it was called as the first argument. It is conventionally given the name,
    32 *self*. Note that *self* is only a convention. You can use any other name, but
    32 *self*. Note that *self* is only a convention. You can use any other name, but
    33 the first argument to the method will always be the same object of the class
    33 the first argument to the method will always be the same object of the class
    34 from which the method was called. The data memebers that belong to the class are
    34 from which the method was called. The data members that belong to the class are
    35 called as *class attributes*. *Class attributes* are preceded by the object of
    35 called as *class attributes*. *Class attributes* are preceded by the object of
    36 the class and a dot. In the above example, *name* is a class attribute since it
    36 the class and a dot. In the above example, *name* is a class attribute since it
    37 is preceded by the *self* object. *Class attributes* can be accessed from
    37 is preceded by the *self* object. *Class attributes* can be accessed from
    38 anywhere within the class. 
    38 anywhere within the class. 
    39 
    39 
    68   Traceback (most recent call last):
    68   Traceback (most recent call last):
    69     File "class.py", line 10, in <module>
    69     File "class.py", line 10, in <module>
    70       print e.name
    70       print e.name
    71   AttributeError: Employee instance has no attribute 'name'
    71   AttributeError: Employee instance has no attribute 'name'
    72 
    72 
       
    73 It is also possible to assign values to the attributes using the above notation::
       
    74 
       
    75   >>> emp = Employee()
       
    76   >>> emp.name = 'John'
       
    77   >>> print emp.name
       
    78   John
       
    79 
       
    80 Magic methods
       
    81 -------------
       
    82 
       
    83 Python reserves a number of names starting and ending with **__**. Note that it
       
    84 is a double underscore. We must not invent names of this kind in our programs.
       
    85 These methods are generally referred to as *Magic methods* or sometimes called
       
    86 as *Special Methods*. Each *Magic method* performs a specific function. One such
       
    87 magic method we will discuss now is **__init__** method. If you are from C++
       
    88 background, the **__init__** method is analogous to the class constructor. This
       
    89 method is called a constructor because, it is implicitly called everytime a new
       
    90 instance of the class is created. So effectively **__init__** method constructs
       
    91 the object from the class and sets up some initial values for the object. Other
       
    92 than the above special properties, the **__init__** method is similar to any other
       
    93 class method. The argument passing rules are same for **__init__** method. Although,
       
    94 since **__init__** is called when the object is created we need to pass the
       
    95 arguments to the class name we call while creating the object. It passes the
       
    96 arguments to the **__init__** method::
       
    97 
       
    98   class Employee:
       
    99     def __init__(self, name):
       
   100       self.name = name
       
   101 
       
   102     def getName(self):
       
   103       return self.name
       
   104 
       
   105   >>> emp = Employee('John')
       
   106   >>> print emp.getName()
       
   107   John
       
   108 
       
   109 
       
   110 Writing Object Oriented Code
       
   111 ----------------------------
       
   112 
       
   113 Object oriented code mainly encompasses three components: Encapsulation, Inheritence and Polymorphism.
       
   114 Lets briefly look at each of them with examples.