In this article, you will learn about Object Oriented Programming (OOP) in Python and its basics and some examples. You all find out in my article!
Object oriented programming
Python is a multi-paradigm programming language. It supports different programming approaches. One of the common approaches to solving programming problems is creating objects. This is called Object Oriented Programming (OOP).
An object will have two characteristics:
- Properties
- Behavior
I have 1 example:
A parrot can be an object, as it has the following properties:
- Name, age, color will be attributes
- running, asking or singing would be behavior
In Python, the OOP concept follows some basic principles as follows:
Class
A class is a blueprint for the object. With the Parrot example above, we can imagine the class
as a sketch of the Parrot. It will contain all the details about the name, color etc. About the Parrot. class
example:
1 2 3 | class Parrot: pass |
We use the keyword class
to define a class Parrot
. From the class
you have to define detailed properties for describing Parrot
.
Object
An object) is an instantiation of a class. When the class is defined it is the description for a defined object.
Here obj
is an object of class Parrot
.
Now, we will start building Parrot
‘s class and objects.
Create classes and objects in Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class Parrot: # class attribute species = "bird" # instance attribute def __init__(self, name, age): self.name = name self.age = age # instantiate the Parrot class blu = Parrot("Blu", 10) woo = Parrot("Woo", 15) # access the class attributes print("Blu is a {}".format(blu.__class__.species)) print("Woo is also a {}".format(woo.__class__.species)) # access the instance attributes print("{} is {} years old".format( blu.name, blu.age)) print("{} is {} years old".format( woo.name, woo.age)) |
Display value:
1 2 3 4 5 | Blu is a bird Woo is also a bird Blu is 10 years old Woo is 15 years old |
In the above program, we created a class with the name Parrot. Then we define the properties. Attributes are a property of an object.
These properties are defined inside the class __init__
method. This is the initialization method that is run first immediately after the object is created.
Next we create instances for the parrot
class. In the above example, blu
and woo
will be the values to refer to the object.
We can access the class property using __class__.species
. Similarly we can also access instance properties using blu.name
and blu.age
Method
Methods are functions defined within the body of a class. They are used to define the behavior of an object.
Create methods in Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class Parrot: # instance attributes def __init__(self, name, age): self.name = name self.age = age # instance method def sing(self, song): return "{} sings {}".format(self.name, song) def dance(self): return "{} is now dancing".format(self.name) # instantiate the object blu = Parrot("Blu", 10) # call our instance methods print(blu.sing("'Happy'")) print(blu.dance()) |
Display value:
1 2 3 | Blu sings 'Happy' Blu is now dancing |
With the above example. We have identified 2 methods as sing()
and dance()
. they are the methods for the blu
object
Inheritance
Inheritance is a way of creating a new class to use the properties of an existing class without modifying it. The newly formed class is a derived class (or subclass). Likewise, an existing class is a base class (or superclass).
Using Inheritance in Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | # parent class class Bird: def __init__(self): print("Bird is ready") def whoisThis(self): print("Bird") def swim(self): print("Swim faster") # child class class Penguin(Bird): def __init__(self): # call super() function super().__init__() print("Penguin is ready") def whoisThis(self): print("Penguin") def run(self): print("Run faster") peggy = Penguin() peggy.whoisThis() peggy.swim() peggy.run() |
Display value:
1 2 3 4 5 6 | Bird is ready Penguin is ready Penguin Swim faster Run faster |
In the above example we create 2 classes: Bird
(is the parent class) and Penguin
(is the subclass). Here the child class will inherit the functions and properties of the parent class. In the above example, from the subclass we can call swim()
from the parent class
Following the above example we was able to modify the behavior of the superclass whoisThis()
. Furthermore it is possible to extend the superclass’s function by creating a run()
Additionally, we use the super ()
function inside the __init __ ()
. This allows us to run the __init __ ()
of the parent class inside the subclass.
Encapsulation
Using OOP in Python, it is possible to restrict access to methods and variables. This prevents the data from being directly modified this might be called Package. In Python, we denote private properties by using underscores as prefixes, using _
or __
Packed data in Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class Computer: def __init__(self): self.__maxprice = 900 def sell(self): print("Selling Price: {}".format(self.__maxprice)) def setMaxPrice(self, price): self.__maxprice = price c = Computer() c.sell() # change the price c.__maxprice = 1000 c.sell() # using setter function c.setMaxPrice(1000) c.sell() |
Display value:
1 2 3 4 | Selling Price: 900 Selling Price: 900 Selling Price: 1000 |
In the above example we have defined a computer
class. We use the __init__()
to set the price for the computer
. We tried to fix the price. However, it is not possible to change the price because in Python __maxprice
is a private property.
So to change the price we need to use the one setMaxPrice
to take the price as an argument.
Polymorphism
Polymorphism in OOP uses a common interface for many data types.
Suppose, we need to color an object, there are many shape options (rectangle, square, circle). However, we can use the same method to color any shape. This concept is called Polymorphism.
Using polymorphism in Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | class Parrot: def fly(self): print("Parrot can fly") def swim(self): print("Parrot can't swim") class Penguin: def fly(self): print("Penguin can't fly") def swim(self): print("Penguin can swim") # common interface def flying_test(bird): bird.fly() #instantiate objects blu = Parrot() peggy = Penguin() # passing the object flying_test(blu) flying_test(peggy) |
Display value:
1 2 3 | Parrot can fly Penguin can't fly |
In the example we have defined 2 classes Parrot
and Penguin
. Each class has a common method called fly()
. However the function will be different.
To use polymorphism. We create a fly_test()
function that accepts any object and calls the fly()
of that object. So when we convert the blu
and peggy
objects, the function is usable and returns the result.
Conclude
Below I have introduced you to Relative Oriented Programming and specific examples. If you have any questions, please leave a comment below.
See more details
https://www.programiz.com/python-programming/object-oriented-programming