Advertisement
#Python - Example of a simple constructor
class Test:
# Defining a simple constructor of class Test, using def keyword
# which also initializes instance variable, message, associated with an object
# by using 'self' keyword i.e. the current object
def __init__(self):
self.message = 'An object is created'
# Defining a simple method which retuns value of instance variable associated with an object
def get_message(self):
return self.message
# Calling the constructor to create the first object
ob1 = Test()
# Calling the method associated with object ob1 to print the value of its associated instance variable, message
print(ob1.get_message())
An object is created
def __init__(self):
#body of constructor
# Python - A class with no constructor defined in it is provided with a default constructor.
class Test:
def message(self):
print('Hello from Test class')
# Calling the default constructor to create an object of class Test
ob = Test()
# Calling the method message() using an object
ob.message()
Hello from Test class
# Python - Implicitly defining the default constructor
class Test:
def __init__(self):
print('Constructor creating an object')
# Calling the defined default constructor
ob1 = Test()
# Calling the defined default constructor
ob2 = Test()
Constructor creating an object
Constructor creating an object
Advertisement
class Test:
# Defining a parameterized constructor of class Test with instance variable message
def __init__(self, message):
self.message = message
# Defining a method of class Test
def get_message(self):
return self.message
# Calling the constructor to create the first object
ob1 = Test('First object is created')
# Calling the constructor to create the second object
ob2 = Test('Second object is created')
# Printing the value of instance variable message associated with object ob1
print(ob1.get_message())
# Printing the value of instance variable message associated with object ob2
print(ob2.get_message())
First object is created
Second object is created
class Test:
def __init__(self, n):
print('Constructor creating an object :', n)
# Calling the already defined constructor
ob = Test(1)
# Calling the already defined constructor
ob = Test(2)
# Calling the no-argument default constructor i.e. invalid
ob = Test()
Constructor creating an object : 1
Constructor creating an object : 2
Traceback (most recent call last):
File "D:/Python Programs/Test.py", line 15, in <module>
ob = Test()
TypeError: __init__() missing 1 required positional argument: 'n'
#Python - A constructor cannot return any value
class Test:
def __init__(self, a, b):
sum = a+b
return sum
# Calling the constructor of Test class with two arguments
ob1 = Test(10,20)
Traceback (most recent call last):
File "D:/Python Programs/Test.py", line 7, in <module>
ob1 = Test(10,20)
TypeError: __init__() should return None, not 'int'
#Python - A constructor cannot return any value
class Test:
def __init__(self, message):
print(message)
return None
# Calling the constructor of Test class with two arguments
ob1 = Test('Hello')
Hello
# Python - A constructor can be inherited
# Superclass i.e. parent class
class Test1:
def __init__(self, name):
print('Creating an object of', name)
# Subclass i.e. child class
class Test2(Test1):
pass
# Creating an object of parent class by calling its constructor
ob1 = Test1('Test1 class')
# Creating an object of child class by calling its constructor, which it inherited from its parent class.
ob2 = Test2('Test2 class')
Creating an object of Test1 class
Creating an object of Test2 class
class Test:
# Defining the first constructor of class Test
def __init__(self, msg1):
print('Constructor creating the : ',msg1)
#return 'hello'
# Defining the second constructor of class Test i.e. last defined constructor
def __init__(self, m1, m2):
print(m1, m2)
# Making a call to thelast defined constructor.
ob1 = Test(1, 'First Object')
# Making a call to the first constructor.
ob = Test('Second Object')
1 First Object
Traceback (most recent call last):
File "D:/Python Programs/Test.py", line 17, in <module>
ob = Test('Second Object')
TypeError: __init__() missing 1 required positional argument: 'm2'
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement