Professional Documents
Culture Documents
PA RT F O U R
3 . 4 D e fi n e t h e c o m p o n e n t o f a n O b j e c t O r i e n t e d fe a t u re s
3 . 4 . 2 D e fi n e t h e c o m p o n e nt o f a n o b j e c t- o r i e nt e d P y t h o n
3.2.3 Use Magic Method of Python Classes
Object Oriented Programming
Object-Oriented Programming (OOP) solve a programming problem is by creating objects.
An object has two characteristics:
Class
Object
Method
Inheritance
Encapsulation
Polymorphism
Class, Object and Method
A class is a blueprint for the object.
An object (instance) is an instantiation of a class.
Methods are functions defined inside the body of a class. They are used to define the behaviors
of an object
Creating Class and Object in Python
2) A class was created with the name Parrot. Then, define the
attributes. The attributes are a characteristic of an object.
2) These attributes are defined inside the __init__ method of the class.
It is the initializer method that is first run as soon as the object is
created.
3) Then, create the instances of the Parrot class. Here, blu and woo are
references (value) to the new objects.
Output:
Inheritance
1) Inheritance is a way of creating a new class for
using details of an existing class without modifying
it. The newly formed class is a derived class (or child
class). Similarly, the existing class is a base class (or
parent class).
2) Two classes i.e. Bird (parent class) and Penguin
(child class). The child class inherits the functions of
parent class. We can see this from the swim()
method.
3) Again, the child class modified the behavior of the
parent class. We can see this from the whoisThis()
method. Furthermore, we extend the functions of
the parent class, by creating a new run() method. Output:
4) Additionally, super() function inside the __init__()
method. This allows us to run the __init__() method
of the parent class inside the child class.
Encapsulation
OOP in Python use to restrict access to methods and variables. This prevents data from direct
modification which is called encapsulation.
In Python, private attributes was denote using underscore as the prefix i.e single _ or double __.
Data Encapsulation in Python
1) A Computer was defined as class.
1) Program above show num+10, the + operator calls the __add__(10) method. Then, if call
num.__add__(5) directly also will give the same result. However, as mentioned before, magic
methods are not meant to be called directly, but internally, through some other methods or actions.
2) Magic methods are most frequently used to define overloaded behaviours of predefined
operators in Python. For instance, arithmetic operators by default operate upon numeric operands.
This means that numeric objects must be used along with operators like +, -, *, /, etc. The + operator
is also defined as a concatenation operator in string, list and tuple classes. We can say that the +
operator is overloaded.
3) In order to make the overloaded behaviour available in your own custom class, the corresponding
magic method should be overridden. For example, in order to use the + operator with objects of a
user-defined class, it should include the __add__() method.
__new__() method
1) __new__() magic method is implicitly called before the __init__() method. The __new__()
method returns a new object, which is then initialized by __init__().
When you create an instance of the Employee class, this output come out.
__str__() method
1) It is overridden to return a printable string representation of any user defined class.
the __str__() method in