Thank you for printing our content at www.domain-name.com.
Please check back soon for new
contents.
Try hands-on Python with (https://programiz.pro/learn/master-python?
36%
off
Programiz PRO! utm_source=sticky-
Claim Discount Now banner&utm_campaign=programiz&utm_medium=referral)
Programiz (https://programiz.pro/learn/master-python?utm_source=nav-
PRO(/) Search tutorials & examples
floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
Python Object Oriented Programming
Python is a versatile programming language that supports various programming
styles, including object-oriented programming (OOP) through the use of objects
and classes.
An object is any entity that has attributes and behaviors. For example, a parrot is
an object. It has
attributes - name, age, color, etc.
behavior - dancing, singing, etc.
Similarly, a class is a blueprint for that object.
Python
Thank you for Class and
printing our Object
content at www.domain-name.com. Please check back soon for new
contents.
Try hands-on Python with (https://programiz.pro/learn/master-python?
36%
classProgramiz
Parrot: PRO!
off
utm_source=sticky-
Claim Discount Now banner&utm_campaign=programiz&utm_medium=referral)
# class attribute
Programiz (https://programiz.pro/learn/master-python?utm_source=nav-
name Search
= "" tutorials & examples
PRO(/) floating&utm_campaign=programiz&utm_medium=referral)
age = 0
www.domain-name.com
# create parrot1 object
parrot1 = Parrot()
parrot1.name = "Blu"
parrot1.age = 10
# create another object parrot2
parrot2 = Parrot()
parrot2.name = "Woo"
parrot2.age = 15
# access attributes
print(f"{parrot1.name} is {parrot1.age} years old")
print(f"{parrot2.name} is {parrot2.age} years old")
Run Code (/python-programming/online-compiler)
Output
Blu is 10 years old
Woo is 15 years old
In the above example, we created a class with the name Parrot with two
attributes: name and age .
Then, we create instances of the Parrot class. Here, parrot1 and parrot2 are
references (value) to our new objects.
We then accessed and assigned different values to the instance attributes using
the objects name and the . notation.
To learn more about classes and objects, visit Python Classes and Objects
(/python-programming/class)
Python
Thank you for Inheritance
printing our content at www.domain-name.com. Please check back soon for new
contents.
Inheritance is a way of creating a new class for using details of an existing class
Try hands-on Python with (https://programiz.pro/learn/master-python?
without
36% modifying
Programiz PRO!it. utm_source=sticky-
off
Claim Discount Now banner&utm_campaign=programiz&utm_medium=referral)
The newly(https://programiz.pro/learn/master-python?utm_source=nav-
Programiz formed class is a derived class (or child class). Similarly, the existing class
Search tutorials & examples
isPRObase floating&utm_campaign=programiz&utm_medium=referral)
a (/) class (or parent class).
www.domain-name.com
Example 2: Use of Inheritance in Python
# base class
class Animal:
def eat(self):
print( "I can eat!")
def sleep(self):
print("I can sleep!")
# derived class
class Dog(Animal):
def bark(self):
print("I can bark! Woof woof!!")
# Create object of the Dog class
dog1 = Dog()
# Calling members of the base class
dog1.eat()
dog1.sleep()
# Calling member of the derived class
dog1.bark();
Run Code (/python-programming/online-compiler)
Output
I can eat!
I can sleep!
I can bark! Woof woof!!
Here,
Thank youdog1 (the object
for printing of derived
our content class Dog ) can access
at www.domain-name.com. members
Please check of the
back soon forbase
new
contents.
class Animal. It's because Dog is inherited from Animal .
Try hands-on Python with (https://programiz.pro/learn/master-python?
36%
Programiz
# Calling
off members utm_source=sticky-
PRO! of the Animal class
Claim Discount Now
dog1.eat() banner&utm_campaign=programiz&utm_medium=referral)
(https://programiz.pro/learn/master-python?utm_source=nav-
dog1.sleep()
Programiz
PRO(/)
Search tutorials & examples
floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
To learn more about inheritance, visit Python Inheritance (/python-
programming/inheritance).
Python Encapsulation
Encapsulation is one of the key features of object-oriented programming.
Encapsulation refers to the bundling of attributes and methods inside a single class.
It prevents outer classes from accessing and changing attributes and methods of a
class. This also helps to achieve data hiding.
In Python, we denote private attributes using underscore as the prefix i.e single _
or double __ . For example,
Thank you for printing our content at www.domain-name.com. Please check back soon for new
contents.
class Computer:
Try hands-on Python with (https://programiz.pro/learn/master-python?
36% def __init__(self):
off
Programiz PRO!
self.__maxprice = 900
utm_source=sticky-
Claim Discount Now banner&utm_campaign=programiz&utm_medium=referral)
def (https://programiz.pro/learn/master-python?utm_source=nav-
Programiz sell(self):
PRO(/) Search tutorials & examples
floating&utm_campaign=programiz&utm_medium=referral)
print("Selling Price: {}".format(self.__maxprice))
www.domain-name.com
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()
Run Code (/python-programming/online-compiler)
Output
Selling Price: 900
Selling Price: 900
Selling Price: 1000
In the above program, we defined a Computer class.
We used __init__() method to store the maximum selling price of Computer . Here,
notice the code
c.__maxprice = 1000
Here, we have tried to modify the value of __maxprice outside of the class.
However, since __maxprice is a private variable, this modification is not seen on the
output.
As shown, to change the value, we have to use a setter function i.e setMaxPrice()
which takes price as a parameter.
Thank you for printing our content at www.domain-name.com. Please check back soon for new
contents.
Polymorphism
Try hands-on Python with (https://programiz.pro/learn/master-python?
36%
Programizis
Polymorphism
off
PRO! utm_source=sticky-
another important concept of object-oriented programming. It
Claim Discount Now banner&utm_campaign=programiz&utm_medium=referral)
simply means more than one form.
Programiz (https://programiz.pro/learn/master-python?utm_source=nav-
PRO(/)
Search tutorials & examples
floating&utm_campaign=programiz&utm_medium=referral)
That is, the same entity (methodwww.domain-name.com
or operator or object) can perform different
operations in different scenarios.
Let's see an example,
class Polygon:
# method to render a shape
def render(self):
print("Rendering Polygon...")
class Square(Polygon):
# renders Square
def render(self):
print("Rendering Square...")
class Circle(Polygon):
# renders circle
def render(self):
print("Rendering Circle...")
# create an object of Square
s1 = Square()
s1.render()
# create an object of Circle
c1 = Circle()
c1.render()
Run Code (/python-programming/online-compiler)
Output
Rendering Square...
Rendering Circle...
In the above example, we have created a superclass: Polygon and two subclasses:
Square and Circle . Notice the use of the render() method.
The you
Thank main
forpurpose ofcontent
printing our the render() method is to render
at www.domain-name.com. the
Please shape.
check backHowever, the
soon for new
contents.
process of rendering a square is different from the process of rendering a circle.
Try hands-on Python with (https://programiz.pro/learn/master-python?
Hence,
36% the render()
Programiz PRO! method behaves differently in different classes. Or, we can
utm_source=sticky-
off
say Claim Discount
render() is polymorphic.
Now banner&utm_campaign=programiz&utm_medium=referral)
Programiz (https://programiz.pro/learn/master-python?utm_source=nav-
PRO(/) Search tutorials & examples
floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
Key Points to Remember:
Object-Oriented Programming makes the program easy to understand as well as
efficient.
Since the class is sharable, the code can be reused.
Data is safe and secure with data abstraction.
Polymorphism allows the same interface for different objects, so programmers
can write efficient code.
Video: Object-oriented Programming in Python
Object-oriented Programming (OOP) in Python (Easy to Understand Guide) #20
Thank you for printing our content at www.domain-name.com. Please check back soon for new
contents. Next Tutorial:
(/python-programming/class)
Python Class
Try hands-on Python with (https://programiz.pro/learn/master-python?
36%
off
Programiz PRO! utm_source=sticky-
Claim Discount NowPrevious Tutorial:
banner&utm_campaign=programiz&utm_medium=referral)
(/python-programming/user-defined-exception)
User-defined Exception
Programiz (https://programiz.pro/learn/master-python?utm_source=nav-
PRO(/) Search tutorials & examples
floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
Share on:
(https://www.facebook.com/sharer/sharer.php? (https://twitter.com/intent/tweet?
u=https://www.programiz.com/python- text=Check%20this%20amazing%20
programming/object-oriented-programming) programming/object-oriented-progra
Did you find this article helpful?
Related Tutorials
Python Tutorial
Polymorphism in Python
Thank you for printing our content at www.domain-name.com. Please check back soon for new
contents.
(/python-programming/polymorphism)
Try hands-on Python with (https://programiz.pro/learn/master-python?
36%
Python Programiz
off Tutorial PRO! utm_source=sticky-
Claim Discount Now banner&utm_campaign=programiz&utm_medium=referral)
Python Inheritance
Programiz (https://programiz.pro/learn/master-python?utm_source=nav-
PRO(/) Search tutorials & examples
floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
(/python-programming/inheritance)
Python Tutorial
Python Objects and Classes
(/python-programming/class)
Python Library
Python super()
(/python-programming/methods/built-in/super)