Sunday, August 30, 2015

Overloading Constructor in Python Classes

In Python, the constructor __init__ can not be overloaded. If you are a Java or C++ programmer, you probably used this facility before because even the standard libraries of these languages use function overloading.

Altough the language does not support constructor overloading, we can follow the factory design pattern for using multiple constructors in Python. In factory design pattern, we define static class members that create new instances with given parameters. In Python, if a function labeled with a @classmethod, then this function belongs to the class rather than an object. This is the same as the static methods in Java.

Now think that we want to write a Python class with three constructors. In first constructor we want to set param1 to a specific value and others to zero. In second constructor we want to set param2 to a specific value and others to zero and goes on.

 class Clazz:  
   def __init__(self):  
     "please use create1, create2 or create3"  
   
   @classmethod  
   def create1(cls, param1):  
     c = Clazz()  
     c.param1 = param1  
     c.param2 = 0  
     c.param3 = 0  
     return c  
   
   @classmethod  
   def create2(cls, param2):  
     c = Clazz()  
     c.param1 = 0  
     c.param2 = param2  
     c.param3 = 0  
     return c  
   
   @classmethod  
   def create3(cls,param3):  
     c = Clazz()  
     c.param1 = 0  
     c.param2 = 0  
     c.param3 = param3  
     return c  
   
   def dump(self):  
     print("Param1 = %d, Param2 = %d, Param3 = %d" %  
       (self.param1,self.param2,self.param3))  



Of course we don't need to set other parameters to zero in all static methods. Lets get the code more compact:


 class Clazz:  
   
   param1 = 0  
   param2 = 0  
   param3 = 0  
   
   def __init__(self):  
     "please use create1, create2 or create3"  
   
   @classmethod  
   def create1(cls, param1):  
     c = Clazz()  
     c.param1 = param1  
     return c  
   
   @classmethod  
   def create2(cls, param2):  
     c = Clazz()  
     c.param2 = param2  
     return c  
   
   @classmethod  
   def create3(cls,param3):  
     c = Clazz()  
     c.param3 = param3  
     return c  
   
   def dump(self):  
     print("Param1 = %d, Param2 = %d, Param3 = %d" %  
       (self.param1,self.param2,self.param3))  
   
   



And now, we can instantiate some objects from this class using different factory methods:


 myc1 = Clazz.create1(5)  
 myc1.dump()  
   
 myc2 = Clazz.create2(10)  
 myc2.dump()  
   
 myc3 = Clazz.create3(50)  
 myc3.dump()  
   
 myc4 = Clazz()  
 myc4.dump()  
   


myc1 calls the first static factory method. As it is expected, only the value of the first parameter is changed. Following methods sets the other parameters only. The output is

 Param1 = 5, Param2 = 0, Param3 = 0  
 Param1 = 0, Param2 = 10, Param3 = 0  
 Param1 = 0, Param2 = 0, Param3 = 50  
 Param1 = 0, Param2 = 0, Param3 = 0  


As we seen at the last line of the output, the object myc4 is created using the __init__constructor and all the parameters have value of zero.

Hope you get fun!

No comments:

Post a Comment

Thanks