Python Constructors
A constructor is a special type of method (function) which is used to initialize the instance members of the class. Constructor can be parameterized and non-parameterized as well. Constructor definition executes when we create object of the class. Constructors also verify that there are enough resources for the object to perform any start-up task.
Creating a Constructor
A constructor is a class function that begins with double underscore (_). The name of the constructor is always the same __init__().
While creating an object, a constructor can accept arguments if necessary. When we create a class without a constructor, Python automatically creates a default constructor that doesn't do anything.
Every class must have a constructor, even if it simply relies on the default constructor.
Python Constructor Example
Let's create a class named ComplexNumber, having two functions __init__() function to initialize the variable and getData() to display the number properly.
See this example:
>>> class ComplexNumber:
def __init__(self,r=0,i=1):
self.real=r;
self.imag=i;
def getDat(self):
print(“(0)+(1)j”.format(self. real,self.imag))
Output :
>>>c1= ComplexNumber(5,6)
>>>c1.getData()
5+6j
>>>
You can create a new attribute for an object and read it well at the time of defining the values. But you can't create the attribute for already defined objects.
See this example:
>>> c1 = ComplexNumber(5,6)
>>> c1.getData()
5+6j
>>> c2 ComplexNumber(11)
>>> c2.attr = 12
>>> (c2.real, c2.imag, c2.attr)
(11, 0, 12)
>>> cl.attr
Traceback (most recent call last):
File "<pyshellt6”, line 1, in <module>
cl.attr
AttributeError: CornplexNumber •
object has no attribute 'attr'
In Python, Constructors can be parameterized and non-parameterized as well. The parameterized constructors are used to set custom value for instance variables that can be used further in the application.
Let's see an example, here, we are creating a non parameterized constructor.
Python Non Parameterized Constructor Example
class Student:
# Constructor - non parameterized
def __init__(self):
print("This is non parametrized constructor")
def show(self,name):
print("Hello",name)
student = Student()
student.show("irfan")
Output:
This is non parametrized constructor
Hello irfan
Let's see an example, here, we are creating a parameterized constructor.
Python Parameterized Constructor Example
class Student:
# Constructor - parameterized
def __init__(self, name):
print("This is parametrized constructor")
self.name = name
def show(self):
print("Hello",self.name)
student = Student("irfan")
student.show()
Output:
This is parametrized constructor
Hello irfan
A constructor is a special type of method (function) which is used to initialize the instance members of the class. Constructor can be parameterized and non-parameterized as well. Constructor definition executes when we create object of the class. Constructors also verify that there are enough resources for the object to perform any start-up task.
Creating a Constructor
A constructor is a class function that begins with double underscore (_). The name of the constructor is always the same __init__().
While creating an object, a constructor can accept arguments if necessary. When we create a class without a constructor, Python automatically creates a default constructor that doesn't do anything.
Every class must have a constructor, even if it simply relies on the default constructor.
Python Constructor Example
Let's create a class named ComplexNumber, having two functions __init__() function to initialize the variable and getData() to display the number properly.
See this example:
>>> class ComplexNumber:
def __init__(self,r=0,i=1):
self.real=r;
self.imag=i;
def getDat(self):
print(“(0)+(1)j”.format(self.
Output :
>>>c1= ComplexNumber(5,6)
>>>c1.getData()
5+6j
>>>
You can create a new attribute for an object and read it well at the time of defining the values. But you can't create the attribute for already defined objects.
See this example:
>>> c1 = ComplexNumber(5,6)
>>> c1.getData()
5+6j
>>> c2 ComplexNumber(11)
>>> c2.attr = 12
>>> (c2.real, c2.imag, c2.attr)
(11, 0, 12)
>>> cl.attr
Traceback (most recent call last):
File "<pyshellt6”, line 1, in <module>
cl.attr
AttributeError: CornplexNumber •
object has no attribute 'attr'
In Python, Constructors can be parameterized and non-parameterized as well. The parameterized constructors are used to set custom value for instance variables that can be used further in the application.
Let's see an example, here, we are creating a non parameterized constructor.
Python Non Parameterized Constructor Example
class Student:
# Constructor - non parameterized
def __init__(self):
print("This is non parametrized constructor")
def show(self,name):
print("Hello",name)
student = Student()
student.show("irfan")
Output:
This is non parametrized constructor
Hello irfan
Let's see an example, here, we are creating a parameterized constructor.
Python Parameterized Constructor Example
class Student:
# Constructor - parameterized
def __init__(self, name):
print("This is parametrized constructor")
self.name = name
def show(self):
print("Hello",self.name)
student = Student("irfan")
student.show()
Output:
This is parametrized constructor
Hello irfan
Thanks for Sharing, Great.
ReplyDeletePython Online Training