Python Object Oriented (5)
Python Object Oriented (5)
• Uses __init__() to set the initial state of the object by assigning the
values of the object’s properties
class GeekforGeeks:
# default constructor
def __init__(test):
test.geek = "GeekforGeeks"
• Instance variables:
• The instance variables are attributes attached to an instance of a class.
• We define instance variables in the constructor (the __init__() method of a
class).
• Class Variables:
• A class variable is a variable that is declared inside of class, but outside of any
instance method or __init()__ method.
Creating Class and Objects
• In the above example, we created a Class with the name Employee.
• Next, we defined two attributes name and salary.
• Next, in the __init__() method, we initialized the value of attributes.
This method is called as soon as the object is created. The init method
initializes the object.
• Finally, from the Employee class, we created two objects, Emma and
Harry.
• Using the object, we can access and modify its attributes.
Creating Class and Objects
Abstraction
• Abstraction is a technique that involves hiding the implementation
details of a class and only exposing the essential features of the
class to the user.
• This allows the user to work with objects of the class without having
to worry about the details of how the class works.
Abstraction
Define a class that can add and subtract two
numbers
What Does if __name__ == "__main__" Do in
Python?
• Python’s if __name__ == "__main__" idiom is just a normal
conditional block:
• It Allows You to Execute Code When the File Runs as a Script, but Not
When It’s Imported as a Module
Python class as Modules
Person.py
Python class as Modules
Exercise 1
• Write a Rectangle class in Python language, allowing you to build a
rectangle with length and width attributes.
• Create a Perimeter() method to calculate the perimeter of the
rectangle and a Area() method to calculate the area of the rectangle.
• Create a method display() that display the length, width, perimeter
and area of an object created using an instantiation on rectangle
class.
Exercise 2
1.Create a Python class called BankAccount which represents a bank
account, having as attributes: accountNumber (numeric type), name
(name of the account owner as string type), balance.
2.Create a constructor with parameters: accountNumber, name, balance.
3.Create a Deposit() method which manages the deposit actions.
4.Create a Withdrawal() method which manages withdrawals actions.
5.Create an bankFees() method to apply the bank fees with a percentage of
5% of the balance account.
6.Create a display() method to display account details.
7.Give the complete code for the BankAccount class.
Encapsulation in Python
• When you create a class, it means you are implementing
encapsulation. A class is an example of encapsulation as it binds all
the data members (instance variables) and methods into a single unit.
• Using inheritance, we can use the existing class to create a new class
instead of recreating it from scratch.
Inheritance
Single Inheritance In Python
• Syntax
print(Employee.__mro__)
__mro__ attribute (a tuple of classes)
• In Python, the base classes of a class are stored in the __mro__
attribute (a tuple of classes).
• When you call a method or want to use an attribute, Python goes
through this tuple from left to right and tries to find that method or
attribute.
• If it doesn't find it, it goes to the next class. If it finds it, it returns the
result, the other classes are not checked.
Multiple Inheritance
• Python uses the __mro__ attribute to look for the methods and
attributes
• The order of the parent classes in this tuple determines which parent's
method will be returned
Inheritance In Python
Inheritance In Python
Use of super() function
• super() function is used to refer to the parent class or superclass
• When more than one derived class are created from a single base this
type of inheritance is called hierarchical inheritance.
Hybrid Inheritance
Inheritance consisting of multiple types of inheritance is called hybrid inheritance.
Polymorphism
• The word polymorphism means having many forms.
• The key difference is the data types and number of arguments used in
function.
• In inheritance, the child class inherits the methods from the parent class.
• It is possible to modify a method in a child class that it has inherited from the parent class.
• This is particularly useful in cases where the method inherited from the parent class doesn’t quite
fit the child class.
• This process of re-implementing a method in the child class is known as Method Overriding.
Polymorphism with Inheritance :
Method Overriding
Polymorphism with Inheritance: Method Overriding
Method Overloading in Python
• Method overloading means creating multiple methods with the same
name but with different return types or parameters.
• Using method overloading, you can perform different operations
with the same function name by passing different arguments.
• Can be achieved through
• Using Default Arguments
• Using Variable Length Arguments
Method Overloading in Python
• Using Default Arguments
Method Overloading in Python
• Using Variable Length Arguments