Introduction to Python Programming/Python Programming - Class and Objects

6. Classes and Objects edit

Python uses object orientation since its inception owing to which fact creating classes, using classes and objects is absolutely easy.

6.1. Creating classes edit

The class statement creates a new class definition. The user provided name for the class follows the keyword class followed by a colon as follows:

   class myclass:
   ‘The documentation of the class can be added here’
   Class objects
   Class methods
   class myclass:
       'This docstring provides for the details of the class defined'
       count=0
       def __init__(self,name,paycheck):
           self.name=name
           self.paycheck=paycheck
           myclass.count+=1
       def displayworkerCount(self):
           print "Total no of worker %d" %myclass.count
       
       def displayworkerName(self):
           print "Name of the user",self.name,"'s salary", self.paycheck
       
   >>> worker1=myclass("Nobody",15000)
   >>> worker2=myclass("SNGET", 10000)
   >>> worker3=myclass("Somebody", 20000)
   >>> worker4=myclass("Everybody",25000)
   >>> worker1.count
   4
   >>> worker1.displayworkerCount()
   Total no of worker 4
   >>> worker1.displayworkerName()
   Name of the user Nobody 's salary 15000
   >>> print myclass.__doc__
   This docstring provides for the details of the class defined
   >>>

The end user can check if the defined class has a particular method or object or instance defined within the class using the hasattr object.

   >>> worker1
   <__main__.myclass instance at 0x017D5990>
   >>> worker1.name
   'Nobody'
   >>> worker1.paycheck
   15000
   >>> hasattr(worker1,'age')
   False
   >>> hasattr(worker1,'displayworkerCount')
   True

The end user can add, modify, set, or delete the attributes of classes and objects at any time using the following methods.

   >>> worker5=myclass("","")
   >>> worker5.name
   
   >>> worker5.paycheck
   
   >>> worker5.name="New Body"
   >>> worker5.paycheck=50000
   >>>
   >>> worker5.name
   'New Body' 
   >>> worker5.paycheck
   50000
   >>> worker1.paycheck
   15000
   >>> worker1.paycheck=55000
   >>> worker1.paycheck
   55000
   >>> setattr(worker1, "paycheck", 60000)
   >>> worker1.paycheck
   60000
   >>> >>>
   >>> delattr(worker1, "name")
   >>> worker1
   <__main__.myclass instance at 0x0222EA80>
   >>> worker1.name
   Traceback (most recent call last):
     File "<pyshell#372>", line 1, in <module>
       worker1.name
   AttributeError: myclass instance has no attribute 'name'

6.2. Class Built-In Attributes edit

By default, all Python classes have the following built-in attributes that can be accessed using the dot operator just like the __doc__ attribute that provides the documentation of the class.

  1. __dict__ : The dictionary of the namespace of the class
  2. __doc__ : Class documentation string or none, if undefined
  3. __name__ : Class name
  4. __module__ : Module name in which the class is defined. This attribute is "__main__" in interactive mode
  5. __bases__ : A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list.

Trying the above methods for the above defined myclass, we have the following result.

   >>> print "The name of the class is :", myclass.__name__
   The name of the class is : myclass
   >>> print "The name of the module is :", myclass.__module__
   The name of the module is : __main__
   >>> print "The bases for the class myclass is :", myclass.__bases__
   The bases for the class myclass is : ()
   >>> print "The dictionary of namespace for myclass are :",myclass.__dict__
   The dictionary of namespace for myclass are : {'count': 4, '__module__': '__main__', 'displayworkerCount': <function
   displayworkerCount at 0x02244B70>, 'displayworkerName': <function displayworkerName at 0x02244BB0>, '__doc__': 'This
   docstring provides for the details of the class defined', '__init__': <function __init__ at 0x02244B30>}
   >>> print "The document string for myclass is: ",myclass.__doc__
   The document string for myclass is:  This docstring provides for the details of the class defined
   >>>