Python Modul 5 - Sivanandha m s
Python Modul 5 - Sivanandha m s
• If we assign P1 to P2, then the two variables are aliases of the same object
• P2=P1
• P1 is P2 => True
• This type of equality is called shallow equality because it compares only the references, not the contents of
the objects
• To compare the contents of the objects –deep equality
def samePoint(p1,p2):
return (p1.x==p2.x) and (p1.y==p2.y)
samePoint(p1,p2)=>True
If the two variables refer to the same object,they have both shallow and deep equality
Copying
• Copying an object is often an alternative to aliasing
• Copy module contains a function called copy that can duplicate any object
>>>import copy
>>>p1=Point()
>>>p1.x=3
>>>p1.y=4
>>>p2=copy.copy(p1)
>>>p1==p2
False
>>>samePoint(p1,p2)True
• Once we import the copy module, we can use the copy method to make a new Point. P1 and p2 are not the
same point, but they contain the same data
• To copy a simple object like a Point, which doesnot contain any embedded objects, copy is sufficient. This
is called shallow copying
• The copy module contains a method named deepcopy that copies not only the object but also any
embedded objects.This operation is called a deep copy
>>>b2=copy.deepcopy(b1)
Now b1 and b2 are completely separate objects
Destructor
• Destructors are called when an object gets destroyed.
• In Python, destructors are not needed as much needed in C++ because Python has a garbage collector that
handles memory management automatically.
• The __del__() method is a known as a destructor method in Python.
• It is called when all references to the object have been deleted i.e when an object is garbage collected.
• Note : A reference to objects is also deleted when the object goes out of reference or when the program
ends.
Syntax of destructor declaration
def __del__(self):
# body of destructor
Destructor Example
# Python program to illustrate destructor
class Employee:
# Initializing
def __init__(self):
print('Employee created.')
# Deleting (Calling destructor)
def __del__(self):
print('Destructor called, Employee deleted.')
obj = Employee()
del obj
print(“Program ENd”)
Output
Employee created
Destructor called, Employee deleted.
Program End
Destructor Example
• The destructor was called after the program ended or when all the references to object are deleted
class Employee:
# Initializing
def __init__(self):
print('Employee created')
# Calling destructor
def __del__(self):
print("Destructor called")
obj=Employee()
print(“Program End..”)
Output
Employee created
Program End..
Destructor called
Inheritance
• Inheritance allows us to define a class that inherits all the methods and properties from another class.
• Parent class is the class being inherited from, also called base class.
• Child class is the class that inherits from another class, also called derived class.
Create a parent class
• Any class can be a parent class, so the syntax is the same as creating any other class:
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
x = Person("John", "Doe")
x.printname()
Create a child class
• To create a class that inherits the functionality from another class, send the parent class as a parameter
when creating the child class:
class Student(Person):
pass
• Note: Use the pass keyword when you do not want to add any other properties or methods to the class.
• Now the Student class has the same properties and methods as the Person class.
x = Student("Mike", "Olsen")
x.printname()
Add the __init__() Function
• So far we have created a child class that inherits the properties and methods from its parent.
• We want to add the __init__() function to the child class (instead of the pass keyword).
class Student(Person):
def __init__(self, fname, lname):
#add properties etc.
• When you add the __init__() function, the child class will no longer inherit the parent's __init__() function.
• Note: The child's __init__() function overrides the inheritance of the parent's __init__() function.
• To keep the inheritance of the parent's __init__() function, add a call to the parent's __init__() function:
class Student(Person):
def __init__(self, fname, lname):
Person.__init__(self, fname, lname)
Use super() function
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
• By using the super() function, you do not have to use the name of the parent element, it will automatically
inherit the methods and properties from its parent.
Add Properties
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
Obj=Student(“John”,”Mathew”,2020)
Obj.welcome()
• If you add a method in the child class with the same name as a function in the parent class, the inheritance
of the parent method will be overridden