Skip to main content

Python Object Oriented Programming

Python Object-Oriented Programming (OOP) is a powerful paradigm that allows developers to structure their code in a modular and organized way, through the use of objects and classes.

note

Main key points about Object-Oriented Programming:

  • OOP makes the program easy to understand
  • OOP allows the reuse of code

The following are some basic concepts for approaching object-oriented programming in Python. But we have more tutorials on OOP in Python!

Python Class and Object

In Python, everything is an object and every object has a state and behaviours.

To create an object you have to:

  1. Define a class
  2. Create an object from that class, i.e. create an instance from that class
note

An object is an instance of a class!

For example, we create a class called Person with attributes name and age.

Example of Person class
class Person:

def __init__(self, name, age):
# instance attributes
self.name = name
self.age = age

and then we can create instances in this way:

# Create person1 object
person1 = Person("Tom", 24)

# Create person2 object
person2 = Person("John", 42)

# Use instances
print(f"{person1.name} is {person1.age} years old")
print(f"{person2.name} is {person2.age} years old")

Output:

Tom is 24 years old
John is 42 years old
note

We accessed different values to the instance attributes using the objects name and the . notation.

note

To learn more about Class, visit Python Class and to learn more about Abstract classes, visit Python Abstract Class

Python Inheritance

Python inheritance allows classes to inherit properties and methods from other classes.

Inheritance creates a hierarchical relationship between classes, where a derived class (also called subclass or child class) can inherit attributes and behaviors from a base class (also called superclass or parent class).

A class can be derived from one (called classic inheritance) or more (called multiple inheritance) superclasses in Python.

For example, the subclass Dog inherits the class Animal:

# base class
class Animal:

def eat(self):
print("Eat!")

def sleep(self):
print("Sleep!")

# derived class
class Dog(Animal):

def bark(self):
print("Woof!")

and so an instance of Dog class can also access members of the Animal parent class (i.e. the methods eat() and sleep() defined in Animal class):

# Create object of the Dog class
doggo = Dog()

# Calling members of the base class
doggo.eat()
doggo.sleep()

# Calling member of the derived class
doggo.bark();

Output

Eat!
Sleep!
Woof!
note

To learn more about Inheritance and Multiple Inheritance, visit Python Inheritance and Multiple Inheritance

Python Encapsulation

Python encapsulation is a fundamental concept in object-oriented programming that involves bundling data and methods within a class and controlling access to them.

It helps to achieve data hiding: it provides a way to hide the internal implementation details of a class and expose only the necessary information to the outside world, thanks to the use of access modifiers.

In Python, private attributes are denoted using underscore as the prefix i.e. single _ or double __.

For example, we define a Smartphone class as follows:

class Smartphone:

def __init__(self):
self.__maxprice = 800

def sell(self):
print("Selling Price: {}".format(self.__maxprice))

def setMaxPrice(self, price):
self.__maxprice = price

s = Smartphone()
s.sell()

# change the price
# Not work because __maxprice is private attribute!
s.__maxprice = 1000
s.sell()

# change price using setter function
s.setMaxPrice(1000)
s.sell()

Output:

Selling Price: 800
Selling Price: 800
Selling Price: 1000

Polymorphism

Python Polymorphism allows objects of different classes to be treated as objects of a common base class.

Polymorphism allows the use of a single interface to represent multiple types of objects, providing flexibility and extensibility in code design.

For example, consider the superclass Animal and the two subclasses Dog and Cat.

class Animal:
def sound(self):
print("Animal Sound!")

class Dog(Animal):
def sound(self):
print("Woof!")

class Cat(Animal):
def sound(self):
print("Meow!")

and so:

a = Animal()
d = Dog()
c = Cat()

a.sound()
d.sound()
c.sound()

Output:

Animal Sound!
Woof!
Meow!
note

To learn more about Polymorphism, visit Python Polymorphism