Constructors in Python
__init__() function. This special function gets called whenever a new object of that class is instantiated.
This type of function is also called constructors in Object Oriented Programming (OOP). We normally use it to initialize all the variables.
Program on default constructor
class sample:
def __init__(self):
self.a=10
self.b=20
def put(self):
print("sum ",self.a+self.b)
x=sample()
x.put()
program on parameterized constructor
class sample:
def __init__(self,x,y):
self.a=x
self.b=y
def put(self):
print("sum ",self.a+self.b)
x=sample(3,2)
x.put()
Deleting Attributes and Objects
Any attribute of an object can be deleted anytime, using the del statement.
Program on del attribute
class sample:
def __init__(self,x,y):
self.a=x
self.b=y
def put(self):
print("sum ",self.a+self.b)
x=sample(3,2)
x.put()
del x # removes from memory
For
More Explanation
&
Online Classes
More Explanation
&
Online Classes
Contact Us:
+919885348743
+919885348743