Constructor
And Destructor In Python classes
Constructors
in python
Syntax
def __init__(self,*arg):
<data members
initialization n>
# A sample class with init method
class Person:
#init method or
constructor
def
__init__(self, name):
self.name=name
# Sample method
def
welcome(self):
print("Hi
sir, I am", self.name)
per=Person('Ramesh Kumar')
per.welcome()
Output
Hi, sir I am Ramesh Kumar
Constructor
Overloading
For example:
class jknum:
def
__init__(self,x=1,y=0):
self.cx=x
self.cy=y
def callme(self):
print("x
value=%d, y value=%d"%(self.cx,self.cy))
b1=jknum(10)
b2=jknum(20,30)
b1.callme()
b2.callme()
Output
x value=10, Y value=0
y value=20, y value=30
Two kinds of
attributes
1.Data Attributes
2.Class Attributes
1.Data Attributes
class Store:
'''A class
store with a dataattributes self.Book_name.'''
def __init__(self,n=0):
self.book_name=n
def
print_name(self):
print("bookname",self.book_name)
2.Class Attributes
Example:
class classattrib:
num=10
def count_fun(self):
self.__class__.num+=1
N1=classattrib()
N2=classattrib()
print("\n num
value...",N1.num)
print("\n num
value...",N2.num)
print("\n num
value...",N1.__class__.num)
print("\n num
value...",N2.__class__.num)
print("\n num value...",
classattrib.num)
N1.count_fun()
N2.count_fun()
print("\n num
value...",N1.num)
print("\n num
value..",N2.num)
print("\n num
value...",N1.__class__.num)
print("\n num
value...",N2.__class__.num)
print("\n num
value...",classattrib.num)
Output
num value... 10
num value... 10
num value... 10
num value... 10
num value... 10
num value... 12
num value... 12
num value... 12
num value... 12
num value... 12
PRIVATE METHODS AND PRIVATE VARIABLES
In python, there is no existence of
"private" instance variables which cannot be accessed expect inside
an object. However, a convention is being followed by most python code and
coders i.e.., a name prefixed with an underscore, for e.g. _Pmember should be
treated as a non-public part of the API or any python code, whether it is a
function ,a method or a data member. It cannot be execute through class object
directly. It can be execute only through member function.
EXAMPLE :
class number:
""" class consist _a and
_b are two private variables and _callme() a private function. It cannot ne
execute through class object directly. It can be execute only through public
function c is public variables it can be access via class object
directly"""
_a=0
_b=0
c=0
def _callme(self,x,y):
self._a=x
self._a=y
def cal(self):
s=self._a+self._b+self.c
return(s)
def maincall(self,x,y):
self._callme(x,y)
obj=number()
obj.c=40
obj.maincall(10,20)
print("sum value = %d "
%obj.cal())
OUTPUT:
Sum value = 60
DATA HIDING AND
OBJECT PRINTING
In python, we use double underscore (_)
before the attributes name and those attributes will not be directly visible
outside.
FOR EXAMPLE:
class MyClass:
#Hidden
member of My Class
_Hidvar
= 0
#A
member method that changes
#_Hidvar
def
add(self,N):
self._Hidvar
=self._Hidvar + N
print(self._Hidvar)
#Driver code
myObject = MyClass()
myObject.add(2)
myObject.add(5)
print(myObject._Hidvar)
OUTPUT
2
7
7
PRINTING
OBJECTS
EXAMPLE #1
class Test:
def
__init__(self,a,b):
self.a=a
self.b=b
def
__repr__(self):
return("test
a:%s b:%s" %(self.a,self.b))
def __str__(self):
return("from
str method of test:a is %s, b is %s" %(self.a,self.b))
#driver code
t=Test(1234,5678)
print(t) #This calls_str_()
print([t]) # This
calls_repr_()
OUTPUT:
from str method of
test:a is 1234, b is 5678
[test a:1234
b:5678]
DESTRUCTOR
FOR EXAMPLE
class sample:
def
__init__(self,x,y):
self.x=x
self.y=y
def
__del__(self):
print("An
object removed from memory")
def
disp_data(self):
print("x
value-{},y value-{}".format(self.x,self.y))
s1=sample(2,4)
s2=sample(4,5)
s3=sample(10,12)
s1.disp_data()
s2.disp_data()
s3.disp_data()
#del command remove one reference and
invoke destructor method _ del _ () automatically
del s1
del s2
del s3
#program end and the output for the
following
OUTPUT
x value-2,y value-4
x value-4,y value-5
x value-10,y value-12
An object removed from memory
An object removed from memory
An object removed from memory
0 comments:
Post a Comment