You are on page 1of 26

CHAPTER 3 : FUNCTION AND OBJECT-ORIENTED PYTHON

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:

Attributes (name, age, color)


Variables of a class that are shared between all of its instances.

Behavior (singing, dancing as behaviour)


Behaviors are actions that can occur on an object
The Component Of An Object-oriented Python

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

1) A class is a blueprint for the object. An object (instance) is an


instantiation of a class.

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.

4) Access the class attribute using __class__.species. Class attributes


Output:
are the same for all instances of a class. Similarly, we access the
instance attributes using blu.name and blu.age. However, instance
attributes are different for every instance of a class.
Creating Methods in Python
1) Methods are functions defined inside the body
of a class. They are used to define the behaviors of
an object

2) Two methods i.e sing() and dance(). These are


called instance methods because they are called
on an instance object i.e blu.

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.

2) Then, __init__() method used to store the


maximum selling price of Computer. We tried to
modify the price. However, we can't change it
because Python treats the __maxprice as private
attributes.

3) To change the value, use a setter function i.e


setMaxPrice() which takes price as a parameter.
Polymorphism
1) ) Polymorphism allows two or more objects to behave like one another, which allows them to
be used interchangeably. For example, if a method or function knows how to paint a Vehicle
object, then it can also paint a Car or Boat object, since they inherit their data and behavior from
the Vehicle
2) Then, Polymorphism is an ability (in OOP) to use a common interface for multiple forms (data
types).
3)Suppose, we need to color a shape, there are multiple shape options (rectangle, square, circle).
However we could use the same method to color any shape. This concept is called
Polymorphism.
Using Polymorphism in Python
1) The program defined two classes
Parrot and Penguin. Each of them have
a common fly() method. However,
their functions are different.

2) To use polymorphism, created a


common interface i.e flying_test()
function that takes any object and calls
the object's fly() method.

3) Thus, when passed the blu and


peggy objects in the flying_test()
function, it ran effectively.
3.2.3 Use Magic Method of Python Classes
Python - Magic Methods
Magic methods in Python are the special methods which add "magic" to your class. It’s always
surrounded by double underscores (e.g. __init__ or __lt__).
Magic methods are not meant to be invoked directly by you, but the invocation happens
internally from the class on a certain action. For example, when two numbers added using the +
operator, internally, the __add__() method will be called.
Dir() Function
Use the dir() function to see the number of magic methods inherited by a class. For example,
the following lists all the attributes and methods defined in the int class.
Example use magic method to add two numbers using the + operator.

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

2) The employee class to return a string representation of its object.


__add__() method
1) a class named distance is defined with two instance attributes - ft and inch. The addition of
these two distance objects is desired to be performed using the overloading + operator.
2) To achieve this, the magic method __add__() is overridden, which performs the addition of
the ft and inch attributes of the two objects. The __str__() method returns the object's string
representation.
__ge__() method
1) The following method is added in the distance class to overload the >= operator.
2) This method gets invoked when the >= operator is used and returns True or False.
Important Magic Methods
Important Magic Methods
Important Magic Methods
Important Magic Methods
Important Magic Methods
Important Magic Methods
Important Magic Methods

You might also like