Software & Finance





Python - Difference Between __init__ and __call__





 

I have given here python program that explains difference between __init__ and __call__. __init__ would be treated as Constructor where as __call__ methods can be called with objects any number of times. Both __init__ and __call__ functions do take default arguments.

This code is tested with Python version 2.6

 

        class CEmployee:
           age = 0
           name = ""
              
           def __init__ (self, name="unassigned", age=-1):
              self.name   = name
              self.age    = age
              
           def __call__ (self, name, age=10):
              self.name   = name
              self.age    = age
              
           def PrintRecord(self):
              print self.name, self.age
        
        
        s1 = CEmployee()
        s1.PrintRecord()        
        s1("Kathir")
        s1.PrintRecord()
        s1("Kathir", 33)
        s1.PrintRecord()

		#output
        #unassigned -1
        #kathir 10
        #kathir 33