天天看點

Python Built-in Functions

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

test code as  followings

[[email protected] python]$ cat call.py 
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
 
 
class CEmployee1:
   age = 0
   name = ""
       
   def __init__ (self, name="unassigned", age=-1):
      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()

s1 = CEmployee1()
s1.PrintRecord()        
s1("Kathir")
s1.PrintRecord()
s1("Kathir", 33)
s1.PrintRecord()