sttp/basic_python/oop.rst
changeset 0 27e1f5bd2774
equal deleted inserted replaced
-1:000000000000 0:27e1f5bd2774
       
     1 Classes and Objects
       
     2 ===================
       
     3 
       
     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
       
     6 specific functionalities. We were able to use this function as many times as we
       
     7 wanted. In addition to functions, Python also higher level of abstractions
       
     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
       
    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
       
    12 a distinction between the two? The answer to this will be given as we walk through
       
    13 the concepts of *Classes* and *Objects*. *Classes* contain the definition for the
       
    14 *Objects*. *Objects* are instances of *Classes*.
       
    15 
       
    16 A class is defined using the keyword **class** followed by the class name, in
       
    17 turn followed by a semicolon. The statements that a *Class* encloses are written
       
    18 in a new block, i.e on the next indentation level::
       
    19 
       
    20   class Employee:
       
    21     def setName(self, name):
       
    22       self.name = name
       
    23 
       
    24     def getName(self):
       
    25       return self.name
       
    26 
       
    27 In the above example, we defined a class with the name Employee. We also defined
       
    28 two methods, setName and getName for this class. It is important to note the
       
    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
       
    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
       
    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
       
    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
       
    37 is preceded by the *self* object. *Class attributes* can be accessed from
       
    38 anywhere within the class. 
       
    39 
       
    40 We can create objects of a class outside the class definition by using the same
       
    41 syntax we use to call a function with no parameters. We can assign this object
       
    42 to a variable::
       
    43 
       
    44   emp = Employee()
       
    45 
       
    46 In the above example, we create an object named *emp* of the class *Employee*.
       
    47 All the attributes and methods of the class can be accessed by the object of the
       
    48 class using the standard notation *object.attribute* or *object.method()*.
       
    49 Although the first parameter of a class method is the self object, it must not
       
    50 be passed as an argument when calling the method. The *self* object is implicitly
       
    51 passed to the method by the Python interpreter. All other arguments passing rules
       
    52 like default arguments, keyword arguments, argument packing and unpacking follow
       
    53 the same rules as those for ordinary Python functions::
       
    54 
       
    55   >>> emp.setName('John')
       
    56   >>> name = emp.getName()
       
    57   >>> print name
       
    58   John
       
    59   >>> print emp.name
       
    60   John
       
    61 
       
    62 If we at all try to access a class attribute before assigning a value to it, i.e
       
    63 before creating it, Python raises the same error as it would raise for the
       
    64 accessing undefined variable::
       
    65 
       
    66   >>> emp = Employee()
       
    67   >>> emp.name
       
    68   Traceback (most recent call last):
       
    69     File "class.py", line 10, in <module>
       
    70       print e.name
       
    71   AttributeError: Employee instance has no attribute 'name'
       
    72