
Before knowing about Copy Constructor, we have to know what Constructor actually is. Constructor is a special member function that is automatically invoked at the time of object creation. In Python, there are the following types of Constructor.
1. Default Constructor
Here, we need not pass any argument at the creation of the object.
2. Parameterized Constructor
Here, we need to pass argument/arguments at the creation of the object.
Here in this article, we will describe Copy Constructor.
Copy Constructor is especially one parameterized constructor.
Python does not support the concept of Constructor overloading. If we try to define the default constructor and the next parameterized constructor within the same class then only the parameterized constructor will remain active but the default constructor will not work. Here is an example:
class CodeWindow:
#Default Constructor
def __init__(self):
self.__a = 10
self.__b = 20
#Parameterized Constructor
def __init__(self,x,y):
self.__a = x
self.__b = y
def display(self):
print(self.__a)
print(self.__b)
print()
obj1 = CodeWindow()
obj1.display()
obj2 = CodeWindow(30,40)
obj2.display()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in
15 print()
16
---> 17 obj1 = CodeWindow()
18 obj1.display()
19 obj2 = CodeWindow(30,40)
TypeError: __init__() missing 2 required positional arguments: 'x' and 'y'
Explanation:
In this example:
We are trying to use two constructors.
a) Default-Constructor
b) Parameterized Constructor
Here, Within the class we have defined firstly a default constructor and then a parameterized constructor. Now, here default constructor will be replaced by parameterized constructor.
So, if we are trying to create object of default constructor, it will generate error. But, interestingly it is not compile time error. So if we create object of default constructor then only error will be generated. Here, that means we can not provide equal priority for both the constructors. As Python does not support Constructor overloading.
If we wish to execute the concept of Copy Constructor, we have to execute the concept of constructor overloading at first. Here is the alternative solution using the Default Argument.
Solution of Constructor overloading using Default argument:
class CodeWindow:
def __init__(self,x=None,y=None):
print("x: ", x)
print("y: ", y)
if x==None and y==None:
print("Default Constructor")
self.__a = 10
self.__b = 20
elif y==None:
print("One-Parameterized Constructor")
self.__a = x
self.__b = 90
else:
print("Two-Parameterized Constructor")
self.__a = x
self.__b = y
def display(self):
print(self.__a)
print(self.__b)
print()
obj1 = CodeWindow() #default constructor
obj1.display()
obj2 = CodeWindow(30) #parameterized constructor
obj2.display()
obj3=CodeWindow(40,50) #parameterized constructor
obj3.display()
Output:
x: None y: None Default Constructor 10 20 x: 30 y: None One-Parameterized Constructor 30 90 x: 40 y: 50 Two-Parameterized Constructor 40 50
Here, we have used the concept of default argument, one-parameterized constructor, and two-parameterized constructor. In the case of the default constructor at the line of constructor definition, x and y will be initialized by None. In the case of a one-parameterized constructor, x and y will be initialized by 30 and None respectively. In the case of a two-parameterized constructor, x and y will be initialized by 40 and 50 respectively.
Alternative way of Copy Constructor:
class CodeWindow:
def __init__(self,x):
if str(type(x)) == "<class '__main__.CodeWindow'>":
print("Copy Constructor")
self.__a = x.__a
else:
print("Parameterized Constructor")
self.__a = x
def display(self):
print(self.__a)
print()
obj1 = CodeWindow(10)
obj1.display()
obj2 = CodeWindow(obj1)
obj2.display()