100% found this document useful (2 votes)
2K views

PYnative Python

This document provides an overview of object-oriented programming (OOP) concepts in Python, including classes, objects, attributes, methods, inheritance, polymorphism, and more. It explains that everything in Python is an object, and a class defines the blueprint for objects. Key concepts covered include using classes to define properties (attributes) and behaviors (methods) of objects, creating class and instance variables, and different types of methods like instance methods, class methods, and static methods. Examples are provided to demonstrate how to define classes and create objects in Python code.

Uploaded by

dhanu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
2K views

PYnative Python

This document provides an overview of object-oriented programming (OOP) concepts in Python, including classes, objects, attributes, methods, inheritance, polymorphism, and more. It explains that everything in Python is an object, and a class defines the blueprint for objects. Key concepts covered include using classes to define properties (attributes) and behaviors (methods) of objects, creating class and instance variables, and different types of methods like instance methods, class methods, and static methods. Examples are provided to demonstrate how to define classes and create objects in Python code.

Uploaded by

dhanu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 369

8/8/22, 8:13 PM Python Object-Oriented Programming (OOP)

Python Object-Oriented Programming (OOP)


In this series, you will learn OOP (Object Oriented Programming) in Python. OOP concepts
include object, classes, constructor and encapsulation, polymorphism, and inheritance.

Python Object-Oriented Programming

This Python OOP series contains the following in-depth tutorial. You can directly read those.

Classes and Objects in Python: You'll understand how to implement object-oriented


programs by creating to create classes and objects.
Constructors in Python: Learn How to create a constructor to initialize an object in
Python. create different types of constructors.
Python Destructors to Destroy the Object: Learn to create a destructor in Python to
release the other resources object were using.
Encapsulation in Python: Learn to implement Encapsulation in Python using class.
implement Data Hiding using public, protected, and private members
Polymorphism in Python: Learn to implement Polymorphism in Python using function
overloading, method overriding, and operator overloading.
Inheritance in Python: Learn to implement inheritance in Python. Also, learn types of
inheritance and MRO (Method Resolution Order).
Python Instance Variables: Learn to create and access instance variables. Modify
values of instance variables. Understand how dynamically we can add or delete instance
variables from the object
Python Instance Methods: Learn to create and call instance methods. Understand how
dynamically we can add or delete instance methods from the object
Python Class Variables: Learn to create, modify and access class variables. Understand
the difference between instance variables and class variables.
Python Class Method: Learn to create and call class methods. Create class method
using the @classmethod decorator and classmethod() function
Python Static Method: Learn to create and call static methods. Create static method
using the @staticmethod decorator and staticmethod() function
Python Class Method vs. Static Method vs. Instance Method: Understand the
difference between all three class methods
Python OOP Exercise: Solve this exercise to practice and understand OOP concepts.

What is Object Oriented Programming in Python


Object-oriented programming (OOP) is a programming paradigm based on the concept of
"objects". The object contains both data and code: Data in the form of properties (often known as
attributes), and code, in the form of methods (actions object can perform).

An object-oriented paradigm is to design the program using classes and objects. Python
programming language supports different programming approaches like functional programming,
modular programming. One of the popular approaches is object-oriented programming (OOP) to
solve a programming problem is by creating objects

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython%2Fobject-oriented-programming%2F 1/8
8/8/22, 8:13 PM Python Object-Oriented Programming (OOP)

Python OOP concepts

An object has the following two characteristics:

Attribute
Behavior

For example, A Car is an object, as it has the following properties:

name, price, color as attributes


breaking, acceleration as behavior

One important aspect of OOP in Python is to create reusable code using the concept of inheritance.
This concept is also known as DRY (Don't Repeat Yourself).

Class and Objects


In Python, everything is an object. A class is a blueprint for the object. To create an object we
require a model or plan or blueprint which is nothing but class.

For example, you are creating a vehicle according to the Vehicle blueprint (template). The plan
contains all dimensions and structure. Based on these descriptions, we can construct a car, truck,
bus, or any vehicle. Here, a car, truck, bus are objects of Vehicle class

A class contains the properties (attribute) and action (behavior) of the object. Properties represent
variables, and the methods represent actions. Hence class includes both variables and methods.

Python Class and Objects

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython%2Fobject-oriented-programming%2F 2/8
8/8/22, 8:13 PM Python Object-Oriented Programming (OOP)

Object is an instance of a class. The physical existence of a class is nothing but an object. In other
words, the object is an entity that has a state and behavior. It may be any real-world object like the
mouse, keyboard, laptop, etc.

Read More: Classes and Objects in Python

Class Attributes and Methods


When we design a class, we use instance variables and class variables.

In Class, attributes can be defined into two parts:

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.

Inside a Class, we can define the following three types of methods.

Instance method: Used to access or modify the object attributes. If we use instance
variables inside a method, such methods are called instance methods.
Class method: Used to access or modify the class state. In method implementation, if
we use only class variables, then such type of methods we should declare as a class
method.
Static method: It is a general utility method that performs a task in isolation. Inside this
method, we don’t use instance or class variable because this static method doesn’t have
access to the class attributes.

Read Python Class Method vs. Static Method vs. Instance Method to understand the difference
between all three class methods.

Creating Class and Objects


In Python, Use the keyword class to define a Class. In the class definition, the first string is
docstring which, is a brief description of the class.

The docstring is not mandatory but recommended to use. We can get docstring using __doc__
attribute. Use the following syntax to create a class.

Syntax
class classname:

'''documentation string'''

class_suite
 Run

Documentation string: represent a description of the class. It is optional.


class_suite: class suite contains class attributes and methods

We can create any number of objects of a class. use the following syntax to create an object of a
class.
reference_variable = classname()
 Run

OOP Example: Creating Class and Object in Python


read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython%2Fobject-oriented-programming%2F 3/8
8/8/22, 8:13 PM Python Object-Oriented Programming (OOP)

class Employee:

# class variables

company_name = 'ABC Company'

# constructor to initialize the object

def __init__(self, name, salary):

# instance variables

self.name = name

self.salary = salary

# instance method

def show(self):

print('Employee:', self.name, self.salary, self.company_name)

# create first object

emp1 = Employee("Harry", 12000)

emp1.show()

# create second object

emp2 = Employee("Emma", 10000)

emp2.show()

 Run

Output:
Employee: Harry 12000 ABC Company

Employee: Emma 10000 ABC Company

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.

instance variables and methods

Constructors in Python
In Python, a constructor is a special type of method used to initialize the object of a Class. The
constructor will be executed automatically when the object is created. If we create three objects, the
constructor is called three times and initialize each object.

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython%2Fobject-oriented-programming%2F 4/8
8/8/22, 8:13 PM Python Object-Oriented Programming (OOP)

The main purpose of the constructor is to declare and initialize instance variables. It can take at
least one argument that is self. The __init()__ method is called the constructor in Python. In other
words, the name of the constructor should be __init__(self).

A constructor is optional, and if we do not provide any constructor, then Python provides the
default constructor. Every class in Python has a constructor, but it's not required to define it.

Read More:

Encapsulation in Python
In Python, encapsulation is a method of wrapping data and functions into a single entity. For
example, A class encapsulates all the data ( methods and variables). Encapsulation means the
internal representation of an object is generally hidden from outside of the object's definition.

Python Encapsulation

Need of Encapsulation

Encapsulation acts as a protective layer. We can restrict access to methods and variables from
outside, and It can prevent the data from being modified by accidental or unauthorized
modification. Encapsulation provides security by hiding the data from the outside world.

Read the complete guide on Encapsulation in Python.

Example: 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.

In Python, we do not have access modifiers, such as public, private, and protected. But we can
achieve encapsulation by using prefix single underscore and double underscore to control access
of variable and method within the Python program.
class Employee:

def __init__(self, name, salary):

# public member

self.name = name

# private member

# not accessible outside of a class

self.__salary = salary

def show(self):

print("Name is ", self.name, "and salary is", self.__salary)

emp = Employee("Jessa", 40000)

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython%2Fobject-oriented-programming%2F 5/8
8/8/22, 8:13 PM Python Object-Oriented Programming (OOP)

emp.show()

# access salary from outside of a class

print(emp.__salary)

 Run

Output:
Name is Jessa and salary is 40000

AttributeError: 'Employee' object has no attribute '__salary'

In the above example, we create a class called Employee. Within that class, we declare two variables
name and __salary. We can observe that the name variable is accessible, but __salary is the private
variable. We cannot access it from outside of class. If we try to access it, we will get an error

Polymorphism in Python
Polymorphism in OOP is the ability of an object to take many forms. In simple words,
polymorphism allows us to perform the same action in many different ways.

Polymorphism is taken from the Greek words Poly (many) and morphism (forms). Polymorphism
defines the ability to take different forms.

For example, The student can act as a student in college, act as a player on the ground, and as a
daughter/brother in the home. Another example in the programming language, the + operator, acts
as a concatenation and arithmetic addition.

Python Polymorphism

Read the complete guide on Polymorphism in Python.

Example: Using Polymorphism in Python


For example, In the below example, calculate_area() instance method created in both Circle and
Rectangle class. Thus, we can create a function that takes any object and calls the object's
calculate_area() method to implement polymorphism. Using this object can perform

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython%2Fobject-oriented-programming%2F 6/8
8/8/22, 8:13 PM Python Object-Oriented Programming (OOP)

Polymorphism with class methods is useful when we want objects to perform the same action in
different ways. In the below example, both objects calculate the area (same action) but in a
different way (different formulas)
class Circle:

pi = 3.14

def __init__(self, redius):

self.radius = redius

def calculate_area(self):

print("Area of circle :", self.pi * self.radius * self.radius)

class Rectangle:

def __init__(self, length, width):

self.length = length

self.width = width

def calculate_area(self):

print("Area of Rectangle :", self.length * self.width)

# function

def area(shape):

# call action

shape.calculate_area()

# create object

cir = Circle(5)

rect = Rectangle(10, 5)

# call common function

area(cir)

area(rect)

 Run

Output:
Area of circle : 78.5

Area of Rectangle : 50

Inheritance In Python
In an Object-oriented programming language, inheritance is an important aspect. In Python,
inheritance is the process of inheriting the properties of the parent class into a child class.

The primary purpose of inheritance is the reusability of code. Using inheritance, we can use the
existing class to create a new class instead of recreating it from scratch.

Syntax
class BaseClass:

Body of base class

class DerivedClass(BaseClass):

Body of derived class


 Run

Read the complete guide on Inheritance in Python

Example: Use of Inheritance in Python


in the below example, From a vehicle class, we are creating a Car class. We don't need to define
common attributes and methods again in Car class. We only need to add those attributes and
methods which are specific to the Car.

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython%2Fobject-oriented-programming%2F 7/8
8/8/22, 8:13 PM Python Object-Oriented Programming (OOP)

In inheritance, the child class acquires all the data members, properties, and functions of the parent
class. Also, a child class can customize any of the parent class methods.
# Base class

class Vehicle:

def __init__(self, name, color, price):

self.name = name

self.color = color

self.price = price

def info(self):

print(self.name, self.color, self.price)

# Child class
class Car(Vehicle):

def change_gear(self, no):

print(self.name, 'change gear to number', no)

# Create object of Car

car = Car('BMW X1', 'Black', 35000)

car.info()

car.change_gear(5)

 Run

Output:
BMW X1 Black 35000

BMW X1 change gear to number 5

Next Steps
Python OOP Exercise

All Python OOP tutorials:

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython%2Fobject-oriented-programming%2F 8/8
8/8/22, 8:14 PM Classes and Objects in Python

Classes and Objects in Python


Python is an object-oriented programming language. This means that almost all the code is
implemented using a special construct called classes. A class is a code template for creating
objects.

After reading this article, you will learn:

What is class and objects in Python


Class attributes and methods
Creating and accessing object properties
Modify and delete an object

What is a Class and Objects in Python?


Class: The class is a user-defined data structure that binds the data members and
methods into a single unit. Class is a blueprint or code template for object creation.
Using a class, you can create as many objects as you want.
Object: An object is an instance of a class. It is a collection of attributes (variables)
and methods. We use the object of a class to perform actions.

Objects have two characteristics: They have states and behaviors (object has attributes and methods
attached to it) Attributes represent its state, and methods represent its behavior. Using its methods,
we can modify its state.

In short, Every object has the following property.

Identity: Every object must be uniquely identified.


State: An object has an attribute that represents a state of an object, and it also reflects
the property of an object.
Behavior: An object has methods that represent its behavior.

Python is an Object-Oriented Programming language, so everything in Python is treated as an


object. An object is a real-life entity. It is the collection of various data and functions that operate
on those data.

For example, If we design a class based on the states and behaviors of a Person, then States can be
represented as instance variables and behaviors as class methods.

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-classes-and-objects%2F 1/10
8/8/22, 8:14 PM Classes and Objects in Python

understand class and objects in Python

A real-life example of class and objects.

Class: Person

State: Name, Sex, Profession


Behavior: Working, Study

Using the above class, we can create multiple objects that depict different states and behavior.

Object 1: Jessa

State:

Name: Jessa
Sex: Female
Profession: Software Engineer

Behavior:

Working: She is working as a software developer at ABC Company

Study: She studies 2 hours a day

Object 2: Jon

State:

Name: Jon
Sex: Male
Profession: Doctor

Behavior:

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-classes-and-objects%2F 2/10
8/8/22, 8:14 PM Classes and Objects in Python

Working: He is working as a doctor

Study: He studies 5 hours a day

As you can see, Jessa is female, and she works as a Software engineer. On the other hand, Jon is a
male, and he is a lawyer. Here, both objects are created from the same class, but they have
different states and behaviors.

Create a Class in Python


In Python, class is defined by using the class keyword. The syntax to create a class is given below.

Syntax
class class_name:

'''This is a docstring. I have created a new class'''

<statement 1>

<statement 2>

<statement N>
 Run

class_name: It is the name of the class


Docstring: It is the first string inside the
class and has a brief description of the class.
Although not mandatory, this is highly recommended.
statements: Attributes and methods

Example: Define a class in Python

In this example, we are creating a Person Class with name, sex, and profession instance variables.
class Person:

def __init__(self, name, sex, profession):

# data members (instance variables)

self.name = name

self.sex = sex

self.profession = profession

# Behavior (instance methods)

def show(self):

print('Name:', self.name, 'Sex:', self.sex, 'Profession:', self.profession)

# Behavior (instance methods)

def work(self):

print(self.name, 'working as a', self.profession)


 Run

Create Object of a Class


An object is essential to work with the class attributes. The object is created using the class name.
When we create an object of the class, it is called instantiation. The object is also called the
instance of a class.

A constructor is a special method used to create and initialize an object of a class. This method is
defined in the class.

In Python, Object creation is divided into two parts in Object Creation and Object initialization

Internally, the __new__ is the method that creates the object


read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-classes-and-objects%2F 3/10
8/8/22, 8:14 PM Classes and Objects in Python

And, using the __init__() method we can implement constructor to initialize the object.

Read More: Constructors in Python

Syntax
<object-name> = <class-name>(<arguments>)
 Run

Below is the code to create the object of a Person class


jessa = Person('Jessa', 'Female', 'Software Engineer')
 Run

The complete example:


class Person:

def __init__(self, name, sex, profession):

# data members (instance variables)

self.name = name

self.sex = sex

self.profession = profession

# Behavior (instance methods)

def show(self):

print('Name:', self.name, 'Sex:', self.sex, 'Profession:', self.profession)

# Behavior (instance methods)

def work(self):

print(self.name, 'working as a', self.profession)

# create object of a class

jessa = Person('Jessa', 'Female', 'Software Engineer')

# call methods

jessa.show()

jessa.work()

 Run

Output:
Name: Jessa Sex: Female Profession: Software Engineer

Jessa working as a Software Engineer

Class Attributes
When we design a class, we use instance variables and class variables.

In Class, attributes can be defined into two parts:

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.

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-classes-and-objects%2F 4/10
8/8/22, 8:14 PM Classes and Objects in Python

Class Attributes in Python

Objects do not share instance attributes. Instead, every object has its copy of the instance attribute
and is unique to each object.

All instances of a class share the class variables. However, unlike instance variables, the value of a
class variable is not varied from object to object.

Only one copy of the static variable will be created and shared between all objects of the class.

Accessing properties and assigning values

An instance attribute can be accessed or modified by using the dot notation:


instance_name.attribute_name.
A class variable is accessed or modified using the class name

Example
class Student:

# class variables

school_name = 'ABC School'

# constructor

def __init__(self, name, age):

# instance variables

self.name = name

self.age = age

s1 = Student("Harry", 12)

# access instance variables

print('Student:', s1.name, s1.age)

# access class variable

print('School name:', Student.school_name)

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-classes-and-objects%2F 5/10
8/8/22, 8:14 PM Classes and Objects in Python

# Modify instance variables

s1.name = 'Jessa'

s1.age = 14

print('Student:', s1.name, s1.age)

# Modify class variables

Student.school_name = 'XYZ School'

print('School name:', Student.school_name)


 Run

Output
Student: Harry 12

School name: ABC School

Student: Jessa 14

School name: XYZ School

Class Methods
In Object-oriented programming, Inside a Class, we can define the following three types of
methods.

Instance method: Used to access or modify the object state. If we use instance


variables inside a method, such methods are called instance methods.
Class method: Used to access or modify the class state. In method implementation, if
we use only class variables, then such type of methods we should declare as a class
method.
Static method: It is a general utility method that performs a task in isolation. Inside this
method, we don’t use instance or class variable because this static method doesn’t have
access to the class attributes.

class method vs static method vs instance method

Instance methods work on the instance level (object level). For example, if we have two objects
created from the student class, They may have different names, marks, roll numbers, etc. Using
instance methods, we can access and modify the instance variables.
read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-classes-and-objects%2F 6/10
8/8/22, 8:14 PM Classes and Objects in Python

A class method is bound to the class and not the object of the class. It can access only class
variables.

Read More: Python Class Method vs. Static Method vs. Instance Method

Example: Define and call an instance method and class method


# class methods demo

class Student:

# class variable

school_name = 'ABC School'

# constructor

def __init__(self, name, age):

# instance variables

self.name = name

self.age = age

# instance method

def show(self):

# access instance variables and class variables

print('Student:', self.name, self.age, Student.school_name)

# instance method

def change_age(self, new_age):

# modify instance variable

self.age = new_age

# class method

@classmethod

def modify_school_name(cls, new_name):

# modify class variable

cls.school_name = new_name

s1 = Student("Harry", 12)

# call instance methods

s1.show()

s1.change_age(14)

# call class method

Student.modify_school_name('XYZ School')

# call instance methods

s1.show()

 Run

Output
Student: Harry 12 ABC School

Student: Harry 14 XYZ School

Class Naming Convention


Naming conventions are essential in any programming language for better readability. If we give a
sensible name, it will save our time and energy later. Writing readable code is one of the guiding
principles of the Python language.

We should follow specific rules while we are deciding a name for the class in Python.

Rule-1: Class names should follow the UpperCaseCamelCase convention


Rule-2: Exception classes should end in “Error“.
Rule-3: If a class is callable (Calling the class from somewhere), in that case, we can
give a class name like a function.
Rule-4: Python’s built-in classes are typically lowercase words

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-classes-and-objects%2F 7/10
8/8/22, 8:14 PM Classes and Objects in Python

pass Statement in Class


In Python, the pass is a null statement. Therefore, nothing happens when the pass statement is
executed.

The pass statement is used to have an empty block in a code because the empty code is not allowed
in loops, function definition, class definition. Thus, the pass statement will results in no operation
(NOP). Generally, we use it as a placeholder when we do not know what code to write or add code
in a future release.

For example, suppose we have a class that is not implemented yet, but we want to implement it in
the future, and they cannot have an empty body because the interpreter gives an error. So use the
pass statement to construct a body that does nothing.

Example
class Demo:

pass
 Run

In the above example, we defined class without a body. To avoid errors while executing it, we
added the pass statement in the class body.

Object Properties
Every object has properties with it. In other words, we can say that object property is an association
between name and value.

For example, a car is an object, and its properties are car color, sunroof, price, manufacture, model,
engine, and so on. Here, color is the name and red is the value. Object properties are nothing but
instance variables.

Object Properties

Modify Object Properties

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-classes-and-objects%2F 8/10
8/8/22, 8:14 PM Classes and Objects in Python

Every object has properties associated with them. We can set or modify the object’s properties after
object initialization by calling the property directly using the dot operator.
Obj.PROPERTY = value
 Run

Example
class Fruit:

def __init__(self, name, color):

self.name = name

self.color = color

def show(self):

print("Fruit is", self.name, "and Color is", self.color)

# creating object of the class

obj = Fruit("Apple", "red")

# Modifying Object Properties

obj.name = "strawberry"

# calling the instance method using the object obj

obj.show()

# Output Fruit is strawberry and Color is red


 Run

Delete object properties


We can delete the object property by using the del keyword. After deleting it, if we try to access it,
we will get an error.
class Fruit:

def __init__(self, name, color):

self.name = name

self.color = color

def show(self):

print("Fruit is", self.name, "and Color is", self.color)

# creating object of the class

obj = Fruit("Apple", "red")

# Deleting Object Properties

del obj.name

# Accessing object properties after deleting

print(obj.name)

# Output: AttributeError: 'Fruit' object has no attribute 'name'


 Run

In the above example, As we can see, the attribute name has been deleted when we try to print or
access that attribute gets an error message.

Delete Objects
In Python, we can also delete the object by using a del keyword. An object can be anything like,
class object, list, tuple, set, etc.

Syntax
del object_name
 Run

Example: Deleting object


read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-classes-and-objects%2F 9/10
8/8/22, 8:14 PM Classes and Objects in Python

class Employee:

depatment = "IT"

def show(self):

print("Department is ", self.depatment)

emp = Employee()

emp.show()

# delete object

del emp

# Accessing after delete object

emp.show()

# Output : NameError: name 'emp' is not defined


 Run

In the above example, we create the object emp of the class Employee. After that, using the del
keyword, we deleted that object.

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-classes-and-objects%2F 10/10
8/8/22, 8:15 PM Constructors in Python

Constructors in Python
Constructor is a special method used to create and initialize an object of a class. On the other
hand, a destructor is used to destroy the object.

After reading this article, you will learn:

How to create a constructor to initialize an object in Python


Different types of constructors
Constructor overloading and chaining

What is Constructor in Python?


In object-oriented programming, A constructor is a special method used to create and initialize
an object of a class. This method is defined in the class.

The constructor is executed automatically at the time of object creation.


The primary use of a constructor is to declare and initialize data member/ instance
variables of a class. The constructor contains a collection of statements (i.e.,
instructions) that executes at the time of object creation to initialize the attributes of an
object.

For example, when we execute obj = Sample(), Python gets to know that obj is an object of class
Sample and calls the constructor of that class to create an object.

Note: In Python, internally, the __new__ is the method that creates the object, and __del__ method is
called to destroy the object when the reference count for that object becomes zero.

In Python, Object creation is divided into two parts in Object Creation and Object initialization

Internally, the __new__ is the method that creates the object


And, using the __init__() method we can implement constructor to initialize the object.

Syntax of a constructor
def __init__(self):

# body of the constructor


 Run

Where,

def: The keyword is used to define function.


__init__() Method: It is a reserved method. This method gets called as soon as an
object of a class is instantiated.
self: The first argument self refers to the current object. It binds the instance to the
__init__() method. It’s usually named self to follow the naming convention.

Note: The __init__() method arguments are optional. We can define a constructor with any
number of arguments.

Example: Create a Constructor in Python

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-constructors%2F 1/9
8/8/22, 8:15 PM Constructors in Python

In this example, we’ll create a Class Student with an instance variable student name. we’ll see how
to use a constructor to initialize the student name at the time of object creation.
class Student:

# constructor

# initialize instance variable

def __init__(self, name):

print('Inside Constructor')

self.name = name

print('All variables initialized')

# instance Method

def show(self):

print('Hello, my name is', self.name)

# create object using constructor

s1 = Student('Emma')
s1.show()
 Run

Output
Inside Constructor

All variables initialized

Hello, my name is Emma

In the above example, an object s1 is created using the constructor


While creating a Student object name is passed as an argument to the __init__() method
to initialize the object.
Similarly, various objects of the Student class can be created by passing different names
as arguments.

Create an object in Python using a constructor

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-constructors%2F 2/9
8/8/22, 8:15 PM Constructors in Python

Note:

For every object, the constructor will be executed only once. For example, if we create
four objects, the constructor is called four times.
In Python, every class has a constructor, but it’s not required to define it explicitly.
Defining constructors in class is optional.
Python will provide a default constructor if no constructor is defined.

Types of Constructors
In Python, we have the following three types of constructors.

Default Constructor
Non-parametrized constructor
Parameterized constructor

Types of constructor

Default Constructor
Python will provide a default constructor if no constructor is defined. Python adds a default
constructor when we do not include the constructor in the class or forget to declare it. It does not
perform any task but initializes the objects. It is an empty constructor without a body.

If you do not implement any constructor in your class or forget to declare it, the Python inserts a
default constructor into your code on your behalf. This constructor is known as the default
constructor.

It does not perform any task but initializes the objects. It is an empty constructor without a body.

Note:

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-constructors%2F 3/9
8/8/22, 8:15 PM Constructors in Python

The default constructor is not present in the source py file. It is inserted into the code
during compilation if not exists. See the below image.
If you implement your constructor, then the default constructor will not be added.

Example:
class Employee:

def display(self):

print('Inside Display')

emp = Employee()

emp.display()

 Run

Output
Inside Display

As you can see in the example, we do not have a constructor, but we can still create an object for
the class because Python added the default constructor during a program compilation.

Non-Parametrized Constructor
A constructor without any arguments is called a non-parameterized constructor. This type of
constructor is used to initialize each object with default values.

This constructor doesn’t accept the arguments during object creation. Instead, it initializes every
object with the same set of values.
class Company:

# no-argument constructor

def __init__(self):

self.name = "PYnative"

self.address = "ABC Street"

# a method for printing data members

def show(self):

print('Name:', self.name, 'Address:', self.address)

# creating object of the class

cmp = Company()

# calling the instance method using the object

cmp.show()
 Run

Output
Name: PYnative Address: ABC Street

As you can see in the example, we do not send any argument to a constructor while creating an
object.

Parameterized Constructor
A constructor with defined parameters or arguments is called a parameterized constructor. We can
pass different values to each object at the time of creation using a parameterized constructor.

The first parameter to constructor is self that is a reference to the being constructed, and the rest of
the arguments are provided by the programmer. A parameterized constructor can have any number
of arguments.

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-constructors%2F 4/9
8/8/22, 8:15 PM Constructors in Python

For example, consider a company that contains thousands of employees. In this case, while creating
each employee object, we need to pass a different name, age, and salary. In such cases, use the
parameterized constructor.

Example:
class Employee:

# parameterized constructor

def __init__(self, name, age, salary):

self.name = name

self.age = age

self.salary = salary

# display object

def show(self):

print(self.name, self.age, self.salary)

# creating object of the Employee class

emma = Employee('Emma', 23, 7500)

emma.show()

kelly = Employee('Kelly', 25, 8500)

kelly.show()

 Run

Output
Emma 23 7500

Kelly 25 8500

In the above example, we define a parameterized constructor which takes three parameters.

Constructor With Default Values


Python allows us to define a constructor with default values. The default value will be used if we
do not pass arguments to the constructor at the time of object creation.

The following example shows how to use the default values with the constructor.

Example
class Student:

# constructor with default values age and classroom

def __init__(self, name, age=12, classroom=7):

self.name = name

self.age = age

self.classroom = classroom

# display Student

def show(self):

print(self.name, self.age, self.classroom)

# creating object of the Student class

emma = Student('Emma')

emma.show()

kelly = Student('Kelly', 13)

kelly.show()

 Run

Output
Emma 12 7

Kelly 13 7

As you can see, we didn’t pass the age and classroom value at the time of object creation, so
default values are used.
read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-constructors%2F 5/9
8/8/22, 8:15 PM Constructors in Python

Self Keyword in Python


As you all know, the class contains instance variables and methods. Whenever we define instance
methods for a class, we use self as the first parameter. Using self, we can access the instance
variable and instance method of the object.

The first argument self refers to the current object.

Whenever we call an instance method through an object, the Python compiler implicitly passes
object reference as the first argument commonly known as self.

It is not mandatory to name the first parameter as a self. We can give any name whatever we like,
but it has to be the first parameter of an instance method.

Example
class Student:

# constructor

def __init__(self, name, age):

self.name = name

self.age = age

# self points to the current object

def show(self):

# access instance variable using self

print(self.name, self.age)

# creating first object

emma = Student('Emma', 12)

emma.show()

# creating Second object

kelly = Student('Kelly', 13)

kelly.show()

 Run

Output
Emma 12

Kelly 13

Constructor Overloading
Constructor overloading is a concept of having more than one constructor with a different
parameters list in such a way so that each constructor can perform different tasks.

For example, we can create a three constructor which accepts a different set of parameters

Python does not support constructor overloading. If we define multiple constructors then, the
interpreter will considers only the last constructor and throws an error if the sequence of the
arguments doesn’t match as per the last constructor. The following example shows the same.

Example
class Student:

# one argument constructor

def __init__(self, name):

print("One arguments constructor")

self.name = name

# two argument constructor

def __init__(self, name, age):

print("Two arguments constructor")

self.name = name

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-constructors%2F 6/9
8/8/22, 8:15 PM Constructors in Python

self.age = age

# creating first object

emma = Student('Emma')

# creating Second object

kelly = Student('Kelly', 13)

 Run

Output
TypeError: __init__() missing 1 required positional argument: 'age'

As you can see in the above example, we defined multiple constructors with different
arguments.
At the time of object creation, the interpreter executed the second constructor because
Python always considers the last constructor.
Internally, the object of the class will always call the last constructor, even if the class
has multiple constructors.
In the example when we called a constructor only with one argument, we got a type
error.

Constructor Chaining
Constructors are used for instantiating an object. The task of the constructor is to assign value to
data members when an object of the class is created.

Constructor chaining is the process of calling one constructor from another constructor. Constructor
chaining is useful when you want to invoke multiple constructors, one after another, by initializing
only one instance.

In Python, constructor chaining is convenient when we are dealing with inheritance. When an
instance of a child class is initialized, the constructors of all the parent classes are first invoked and
then, in the end, the constructor of the child class is invoked.

Using the super() method we can invoke the parent class constructor from a child class.

Example
class Vehicle:

# Constructor of Vehicle

def __init__(self, engine):

print('Inside Vehicle Constructor')

self.engine = engine

class Car(Vehicle):

# Constructor of Car

def __init__(self, engine, max_speed):

super().__init__(engine)

print('Inside Car Constructor')

self.max_speed = max_speed

class Electric_Car(Car):

# Constructor of Electric Car

def __init__(self, engine, max_speed, km_range):

super().__init__(engine, max_speed)

print('Inside Electric Car Constructor')

self.km_range = km_range

# Object of electric car

ev = Electric_Car('1500cc', 240, 750)

print(f'Engine={ev.engine}, Max Speed={ev.max_speed}, Km range={ev.km_range}')

 Run

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-constructors%2F 7/9
8/8/22, 8:15 PM Constructors in Python

Output
Inside Vehicle Constructor

Inside Car Constructor

Inside Electric Car Constructor

Engine=1500cc, Max Speed=240, Km range=750

Counting the Number of objects of a Class


The constructor executes when we create the object of the class. For every object, the constructor is
called only once. So for counting the number of objects of a class, we can add a counter in the
constructor, which increments by one after each object creation.

Example
class Employee:

count = 0

def __init__(self):

Employee.count = Employee.count + 1

# creating objects

e1 = Employee()

e2 = Employee()

e2 = Employee()

print("The number of Employee:", Employee.count)

 Run

Output
The number of employee: 3

Constructor Return Value


In Python, the constructor does not return any value. Therefore, while declaring a constructor, we
don’t have anything like return type. Instead, a constructor is implicitly called at the time of object
instantiation. Thus, it has the sole purpose of initializing the instance variables.

The __init__() is required to return None. We can not return something else. If we try to return a
non-None value from the __init__() method, it will raise TypeError.

Example
class Test:

def __init__(self, i):

self.id = i

return True

d = Test(10)

 Run

Output
TypeError: __init__() should return None, not 'bool'

Conclusion and Quick recap


In this lesson, we learned constructors and used them in object-oriented programming to design
classes and create objects.

The below list contains the summary of the concepts we learned in this tutorial.
read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-constructors%2F 8/9
8/8/22, 8:15 PM Constructors in Python

A constructor is a unique method used to initialize an object of the class.


Python will provide a default constructor if no constructor is defined.
Constructor is not a method and doesn’t return anything. it returns None
In Python, we have three types of constructor default, Non-parametrized, and
parameterized constructor.
Using self, we can access the instance variable and instance method of the object. The
first argument self refers to the current object.
Constructor overloading is not possible in Python.
If the parent class doesn’t have a default constructor, then the compiler would not insert
a default constructor in the child class.
A child class constructor can also invoke the parent class constructor using the super()
method.

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-constructors%2F 9/9
8/8/22, 8:15 PM Python Destructors to Destroy the Object

Python Destructors to Destroy the Object


Destructor is a special method that is called when an object gets destroyed. On the other hand,
a constructor is used to create and initialize an object of a class.

After reading this article, you will learn:

How create a destructor in Python


The use of __del__() method
Wokring of a destructor

What is Destructor in Python?


In object-oriented programming, A destructor is called when an object is deleted or destroyed.
Destructor is used to perform the clean-up activity before destroying the object, such as closing
database connections or filehandle.

Python has a garbage collector that handles memory management automatically. For example, it
cleans up the memory when an object goes out of scope.

But it’s not just memory that has to be freed when an object is destroyed. We must release or close
the other resources object were using, such as open files, database connections, cleaning up the
buffer or cache. To perform all those cleanup tasks we use destructor in Python.

The destructor is the reverse of the constructor. The constructor is used to initialize objects, while
the destructor is used to delete or destroy the object that releases the resource occupied by the
object.

In Python, destructor is not called manually but completely automatic. destructor gets called in
the following two cases

When an object goes out of scope or


The reference counter of the object reaches 0.

In Python, The special method __del__() is used to define a destructor. For example, when we


execute del object_name destructor gets called automatically and the object gets garbage collected.

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-destructor%2F 1/7
8/8/22, 8:15 PM Python Destructors to Destroy the Object

Python destructor to destroy an object

Create Destructor using the __del__() Method


The magic method __del__() is used as the destructor in Python. The __del__() method will be
implicitly invoked when all references to the object have been deleted, i.e., is when an object is
eligible for the garbage collector.

This method is automatically called by Python when the instance is about to be destroyed. It is also
called a finalizer or (improperly) a destructor.

Syntax of destructor declaration


def __del__(self):

# body of a destructor
 Run

Where,

def: The keyword is used to define a method.

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-destructor%2F 2/7
8/8/22, 8:15 PM Python Destructors to Destroy the Object

__del__() Method: It is a reserved method. This method gets called as soon as all


references to the object have been deleted
self: The first argument self refers to the current object.

Note: The __del__() method arguments are optional. We can define a destructor with any number
of arguments.

Example

Let’s see how to create a destructor in Python with a simple example. In this example, we’ll create
a Class Student with a destructor. We’ll see: –

How to implement a destructor


how destructor gets executed when we delete the object.

class Student:

# constructor

def __init__(self, name):

print('Inside Constructor')

self.name = name

print('Object initialized')

def show(self):

print('Hello, my name is', self.name)

# destructor

def __del__(self):

print('Inside destructor')

print('Object destroyed')

# create object

s1 = Student('Emma')
s1.show()

# delete object

del s1

 Run

Output
Inside Constructor

Object initialized

Hello, my name is Emma

Inside destructor

Object destroyed

Note:

As you can see in the output, the __del__() method get called automatically is called when we
deleted the object reference using del s1.

In the above code, we created one object. The s1 is the reference variable that is pointing to the
newly created object.

The destructor has called when the reference to the object is deleted or the reference count for the
object becomes zero

Important Points to Remember about Destructor


read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-destructor%2F 3/7
8/8/22, 8:15 PM Python Destructors to Destroy the Object

The __del__ method is called for any object when the reference count for that object
becomes zero.
The reference count for that object becomes zero when the application ends, or we
delete all references manually using the del keyword.
The destructor will not invoke when we delete object reference. It will only invoke
when all references to the objects get deleted.

Working of destructor

Example:

Let’s understand the above points using the example.

First create object of a student class using s1 = student('Emma')


Next, create a new object reference s2 by assigning s1 to s2 using s2=s1
Now, both reference variables s1 and s2 point to the same object.
Next, we deleted reference s1
Next, we have added 5 seconds of sleep to the main thread to understand that
destructors only invoke when all references to the objects get deleted.

import time

class Student:

# constructor

def __init__(self, name):

print('Inside Constructor')

self.name = name

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-destructor%2F 4/7
8/8/22, 8:15 PM Python Destructors to Destroy the Object

def show(self):

print('Hello, my name is', self.name)

# destructor

def __del__(self):

print('Object destroyed')

# create object

s1 = Student('Emma')
# create new reference

# both reference points to the same object

s2 = s1

s1.show()

# delete object reference s1

del s1

# add sleep and observe the output

time.sleep(5)

print('After sleep')

s2.show()
 Run

Output:
Inside Constructor

Hello, my name is Emma

After Sleep
After sleep

Hello, my name is Emma

Object destroyed

As you can see in the output destructors only invoked when all references to the objects
get deleted.
Also, the destructor is executed when the code (application) ends and the object is
available for the garbage collector. (I.e., we didn’t delete object reference s2 manually
using del s2).

Cases when Destructor doesn’t work Correctly


The __del__ is not a perfect solution to clean up a Python object when it is no longer required. In
Python, the destructor behave behaves weirdly and doesn’t execute in the following two cases.

1. Circular referencing when two objects refer to each other


2. Exception occured in __init__() method

Circular Referencing
The __del()__() doesn’t work correctly in the case of circular referencing. In circular referencing
occurs when two objects refer to each other.

When both objects go out of scope, Python doesn’t know which object to destroy first. So, to avoid
any errors, it doesn’t destroy any of them.

In short, it means that the garbage collector does not know the order in which the object should be
destroyed, so it doesn’t delete them from memory.

Ideally, the destructor must execute when an object goes out of scope, or its reference count reaches
zero.
read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-destructor%2F 5/7
8/8/22, 8:15 PM Python Destructors to Destroy the Object

But the objects involved in this circular reference will remain stored in the memory as long as the
application will run.

Example:

In the below example, ideally, both Vehicle and Car objects must be destroyed by the garbage
collector after they go out of scope. Still, because of the circular reference, they remain in memory.

I’d recommend using Python’s with statement for managing resources that need to be cleaned up.
import time

class Vehicle():

def __init__(self, id, car):

self.id = id;

# saving reference of Car object

self.dealer = car;

print('Vehicle', self.id, 'created');

def __del__(self):

print('Vehicle', self.id, 'destroyed');

class Car():

def __init__(self, id):

self.id = id;

# saving Vehicle class object in 'dealer' variable

# Sending reference of Car object ('self') for Vehicle object

self.dealer = Vehicle(id, self);

print('Car', self.id, 'created')

def __del__(self):

print('Car', self.id, 'destroyed')

# create car object

c = Car(12)

# delete car object

del c

# ideally destructor must execute now

# to observe the behavior

time.sleep(8)
 Run

Output:
Vehicle 12 created

Car 12 created

Exception in __init__ Method


In object-oriented programming, A constructor is a special method used to create and initialize an
object of a class. using the __init__() method we can implement a constructor to initialize the
object.

In OOP, if any exception occurs in the constructor while initializing the object, the constructor
destroys the object.

Likewise, in Python, if any exception occurs in the init method while initializing the object, the
method del gets called. But actually, an object is not created successfully, and resources are not
allocated to it

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-destructor%2F 6/7
8/8/22, 8:15 PM Python Destructors to Destroy the Object

even though the object was never initialized correctly, the del method will try to empty all the
resources and, in turn, may lead to another exception.

Example:
class Vehicle:

def __init__(self, speed):

if speed > 240:

raise Exception('Not Allowed');

self.speed = speed;

def __del__(self):

print('Release resources')

# creating an object

car = Vehicle(350);

# to delete the object explicitly

del car

 Run

Output:
Traceback (most recent call last):

Release resources

Exception: Not Allowed

Summary and Quick Recap


In object-oriented programming, A destructor is called when an object is deleted or
destroyed.
Destructor is used to perform the clean-up activity before destroying the object, such as
closing database connections or filehandle.
In Python we use __del__() method to perform clean-up task before deleting the object.
The destructor will not invoke when we delete object reference. It will only invoke
when all references to the objects get deleted.

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-destructor%2F 7/7
8/8/22, 8:16 PM Encapsulation in Python

Encapsulation in Python
Encapsulation is one of the fundamental concepts in object-oriented programming (OOP),
including abstraction, inheritance, and polymorphism. This lesson will cover what encapsulation is
and how to implement it in Python.

After reading this article, you will learn:

Encapsulation in Python
Need for Encapsulation
Data Hiding using public, protected, and private members
Data Hiding vs. Encapsulation
Getter and Setter Methods
Benefits of Encapsulation

What is Encapsulation in Python?


Encapsulation in Python describes the concept of bundling data and methods within a single
unit. So, for example, 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.

Implement encapsulation using a class

Example:

In this example, we create an Employee class by defining employee attributes such as name and
salary as an instance variable and implementing behavior using work() and show() instance
methods.
class Employee:

# constructor

def __init__(self, name, salary, project):

# data members

self.name = name

self.salary = salary

self.project = project

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-encapsulation%2F 1/7
8/8/22, 8:16 PM Encapsulation in Python

# method

# to display employee's details

def show(self):

# accessing public data member

print("Name: ", self.name, 'Salary:', self.salary)

# method

def work(self):

print(self.name, 'is working on', self.project)

# creating object of a class

emp = Employee('Jessa', 8000, 'NLP')

# calling public method of the class

emp.show()

emp.work()
 Run

Output:
Name: Jessa Salary: 8000

Jessa is working on NLP

Using encapsulation, we can hide an object’s internal representation from the outside. This is called
information hiding.

Also, encapsulation allows us to restrict accessing variables and methods directly and prevent
accidental data modification by creating private data members and methods within a class.

Encapsulation is a way to can restrict access to methods and variables from outside of class.
Whenever we are working with the class and dealing with sensitive data, providing access to all
variables used within the class is not a good choice.

For example, Suppose you have an attribute that is not visible from the outside of an object and
bundle it with methods that provide read or write access. In that case, you can hide specific
information and control access to the object’s internal state. Encapsulation offers a way for us to
access the required variable without providing the program full-fledged access to all variables of a
class. This mechanism is used to protect the data of an object from other objects.

Access Modifiers in Python


Encapsulation can be achieved by declaring the data members and methods of a class either as
private or protected. But In Python, we don’t have direct access modifiers like public, private, and
protected. We can achieve this by using single underscore and double underscores.

Access modifiers limit access to the variables and methods of a class. Python provides three types
of access modifiers private, public, and protected.

Public Member: Accessible anywhere from otside oclass.


Private Member: Accessible within the class
Protected Member: Accessible within the class and its sub-classes

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-encapsulation%2F 2/7
8/8/22, 8:16 PM Encapsulation in Python

Data hiding using access modifiers

Public Member
Public data members are accessible within and outside of a class. All member variables of the class
are by default public.

Example:
class Employee:

# constructor

def __init__(self, name, salary):

# public data members

self.name = name

self.salary = salary

# public instance methods

def show(self):

# accessing public data member

print("Name: ", self.name, 'Salary:', self.salary)

# creating object of a class

emp = Employee('Jessa', 10000)

# accessing public data members

print("Name: ", emp.name, 'Salary:', emp.salary)

# calling public method of the class

emp.show()
 Run

Output
Name: Jessa Salary: 10000

Name: Jessa Salary: 10000

Private Member
We can protect variables in the class by marking them private. To define a private variable add two
underscores as a prefix at the start of a variable name.

Private members are accessible only within the class, and we can’t access them directly from the
class objects.
read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-encapsulation%2F 3/7
8/8/22, 8:16 PM Encapsulation in Python

Example:
class Employee:

# constructor

def __init__(self, name, salary):

# public data member

self.name = name

# private member

self.__salary = salary

# creating object of a class

emp = Employee('Jessa', 10000)

# accessing private data members

print('Salary:', emp.__salary)
 Run

Output
AttributeError: 'Employee' object has no attribute '__salary'

In the above example, the salary is a private variable. As you know, we can’t access the private
variable from the outside of that class.

We can access private members from outside of a class using the following two approaches

Create public method to access private members


Use name mangling

Let’s see each one by one

Public method to access private members


Example: Access Private member outside of a class using an instance method
class Employee:

# constructor

def __init__(self, name, salary):

# public data member

self.name = name

# private member

self.__salary = salary

# public instance methods

def show(self):

# private members are accessible from a class

print("Name: ", self.name, 'Salary:', self.__salary)

# creating object of a class

emp = Employee('Jessa', 10000)

# calling public method of the class

emp.show()
 Run

Output:
Name: Jessa Salary: 10000

Name Mangling to access private members


We can directly access private and protected variables from outside of a class through name
mangling. The name mangling is created on an identifier by adding two leading underscores and
one trailing underscore, like this _classname__dataMember, where classname is the current class, and
data member is the private variable name.
read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-encapsulation%2F 4/7
8/8/22, 8:16 PM Encapsulation in Python

Example: Access private member


class Employee:

# constructor

def __init__(self, name, salary):

# public data member

self.name = name

# private member

self.__salary = salary

# creating object of a class

emp = Employee('Jessa', 10000)

print('Name:', emp.name)

# direct access to private member using name mangling

print('Salary:', emp._Employee__salary)
 Run

Output
Name: Jessa

Salary: 10000

Protected Member
Protected members are accessible within the class and also available to its sub-classes. To define a
protected member, prefix the member name with a single underscore _.

Protected data members are used when you implement inheritance and want to allow data members
access to only child classes.

Example: Proctecd member in inheritance.


# base class

class Company:

def __init__(self):

# Protected member

self._project = "NLP"

# child class
class Employee(Company):

def __init__(self, name):

self.name = name

Company.__init__(self)

def show(self):

print("Employee name :", self.name)

# Accessing protected member in child class

print("Working on project :", self._project)

c = Employee("Jessa")

c.show()

# Direct access protected data member

print('Project:', c._project)
 Run

Output
Employee name : Jessa

Working on project : NLP

Project: NLP

Getters and Setters in Python

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-encapsulation%2F 5/7
8/8/22, 8:16 PM Encapsulation in Python

To implement proper encapsulation in Python, we need to use setters and getters. The primary
purpose of using getters and setters in object-oriented programs is to ensure data encapsulation.
Use the getter method to access data members and the setter methods to modify the data members.

In Python, private variables are not hidden fields like in other programming languages. The getters
and setters methods are often used when:

When we want to avoid direct access to private variables


To add validation logic for setting a value

Example
class Student:

def __init__(self, name, age):

# private member

self.name = name

self.__age = age

# getter method

def get_age(self):

return self.__age

# setter method

def set_age(self, age):


self.__age = age

stud = Student('Jessa', 14)

# retrieving age using getter

print('Name:', stud.name, stud.get_age())

# changing age using setter

stud.set_age(16)

# retrieving age using getter

print('Name:', stud.name, stud.get_age())


 Run

Output
Name: Jessa 14

Name: Jessa 16

Let’s take another example that shows how to use encapsulation to implement information hiding
and apply additional validation before changing the values of your object attributes (data member).

Example: Information Hiding and conditional logic for setting an object attributes
class Student:

def __init__(self, name, roll_no, age):

# private member

self.name = name

# private members to restrict access

# avoid direct data modification

self.__roll_no = roll_no

self.__age = age

def show(self):

print('Student Details:', self.name, self.__roll_no)

# getter methods

def get_roll_no(self):

return self.__roll_no

# setter method to modify data member

# condition to allow data modification with rules

def set_roll_no(self, number):

if number > 50:

print('Invalid roll no. Please set correct roll number')

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-encapsulation%2F 6/7
8/8/22, 8:16 PM Encapsulation in Python

else:

self.__roll_no = number

jessa = Student('Jessa', 10, 15)

# before Modify

jessa.show()

# changing roll number using setter

jessa.set_roll_no(120)

jessa.set_roll_no(25)

jessa.show()
 Run

Output:
Student Details: Jessa 10

Invalid roll no. Please set correct roll number

Student Details: Jessa 25

Advantages of Encapsulation
Security: The main advantage of using encapsulation is the security of the data.
Encapsulation protects an object from unauthorized access. It allows private and
protected access levels to prevent accidental data modification.
Data Hiding: The user would not be knowing what is going on behind the scene. They
would only be knowing that to modify a data member, call the setter method. To read a
data member, call the getter method. What these setter and getter methods are doing is
hidden from them.
Simplicity: It simplifies the maintenance of the application by keeping classes separated
and preventing them from tightly coupling with each other.
Aesthetics: Bundling data and methods within a class makes code more readable and
maintainable

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-encapsulation%2F 7/7
8/8/22, 8:17 PM Polymorphism in Python

Polymorphism in Python
Object-Oriented Programming (OOP) has four essential characteristics: abstraction, encapsulation,
inheritance, and polymorphism.

This lesson will cover what polymorphism is and how to implement them in Python. Also, you’ll
learn how to implement polymorphism using function overloading, method overriding, and
operator overloading.

What is Polymorphism in Python?


Polymorphism in Python is the ability of an object to take many forms. In simple words,
polymorphism allows us to perform the same action in many different ways.

For example, Jessa acts as an employee when she is at the office. However, when she is at home,
she acts like a wife. Also, she represents herself differently in different places. Therefore, the same
person takes different forms as per the situation.

A person takes different forms

In polymorphism, a method can process objects differently depending on the class type or data
type. Let’s see simple examples to understand it better.

Polymorphism in Built-in function len()


The built-in function len() calculates the length of an object depending upon its type. If an object
is a string, it returns the count of characters, and If an object is a list, it returns the count of items in
a list.

The len() method treats an object as per its class type.

Example:
students = ['Emma', 'Jessa', 'Kelly']

school = 'ABC School'

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-polymorphism%2F 1/10
8/8/22, 8:17 PM Polymorphism in Python

# calculate count

print(len(students))
print(len(school))
 Run

Output
3

10

Polymorphic len() function

Polymorphism With Inheritance


Polymorphism is mainly used with inheritance. In inheritance, child class inherits the attributes
and methods of a parent class. The existing class is called a base class or parent class, and the new
class is called a subclass or child class or derived class.

Using method overriding polymorphism allows us to defines methods in the child class that have
the same name as the methods in the parent class. This process of re-implementing the inherited
method in the child class is known as Method Overriding.

Advantage of method overriding

It is effective when we want to extend the functionality by altering the inherited method.
Or the method inherited from the parent class doesn’t fulfill the need of a child class, so
we need to re-implement the same method in the child class in a different way.
Method overriding is useful when a parent class has multiple child classes, and one of
that child class wants to redefine the method. The other child classes can use the parent
class method. Due to this, we don’t need to modification the parent class code

In polymorphism, Python first checks the object’s class type and executes the appropriate
method when we call the method. For example, If you create the Car object, then Python calls the
speed() method from a Car class.
read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-polymorphism%2F 2/10
8/8/22, 8:17 PM Polymorphism in Python

Let’s see how it works with the help of an example.

Example: Method Overriding


In this example, we have a vehicle class as a parent and a ‘Car’ and ‘Truck’ as its sub-class. But
each vehicle can have a different seating capacity, speed, etc., so we can have the same instance
method name in each class but with a different implementation. Using this code can be extended
and easily maintained over time.

Polymorphism with Inheritance

class Vehicle:

def __init__(self, name, color, price):

self.name = name

self.color = color

self.price = price

def show(self):

print('Details:', self.name, self.color, self.price)

def max_speed(self):

print('Vehicle max speed is 150')

def change_gear(self):

print('Vehicle change 6 gear')

# inherit from vehicle class

class Car(Vehicle):

def max_speed(self):

print('Car max speed is 240')

def change_gear(self):

print('Car change 7 gear')

# Car Object

car = Car('Car x1', 'Red', 20000)

car.show()

# calls methods from Car class

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-polymorphism%2F 3/10
8/8/22, 8:17 PM Polymorphism in Python

car.max_speed()

car.change_gear()

# Vehicle Object

vehicle = Vehicle('Truck x1', 'white', 75000)

vehicle.show()

# calls method from a Vehicle class

vehicle.max_speed()

vehicle.change_gear()
 Run

Output:
Details: Car x1 Red 20000

Car max speed is 240

Car change 7 gear

Details: Truck x1 white 75000

Vehicle max speed is 150

Vehicle change 6 gear

As you can see, due to polymorphism, the Python interpreter recognizes that the max_speed() and
change_gear() methods are overridden for the car object. So, it uses the one defined in the child
class (Car)

On the other hand, the show() method isn’t overridden in the Car class, so it is used from the
Vehicle class.

Overrride Built-in Functions


In Python, we can change the default behavior of the built-in functions. For example, we can
change or extend the built-in functions such as len(), abs(), or divmod() by redefining them in our
class. Let’s see the example.

Example

In this example, we will redefine the function len()


class Shopping:

def __init__(self, basket, buyer):

self.basket = list(basket)

self.buyer = buyer

def __len__(self):

print('Redefine length')

count = len(self.basket)

# count total items in a different way

# pair of shoes and shir+pant

return count * 2

shopping = Shopping(['Shoes', 'dress'], 'Jessa')

print(len(shopping))
 Run

Output
Redefine length

Polymorphism In Class methods


Polymorphism with class methods is useful when we group different objects having the same
method. we can add them to a list or a tuple, and we don’t need to check the object type before
calling their methods. Instead, Python will check object type at runtime and call the correct
read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-polymorphism%2F 4/10
8/8/22, 8:17 PM Polymorphism in Python

method. Thus, we can call the methods without being concerned about which class type each object
is. We assume that these methods exist in each class.

Python allows different classes to have methods with the same name.

Let’s design a different class in the same way by adding the same methods in two or
more classes.
Next, create an object of each class
Next, add all objects in a tuple.
In the end, iterate the tuple using a for loop and call methods of a object without
checking its class.

Example

In the below example, fuel_type() and max_speed() are the instance methods created in both
classes.
class Ferrari:

def fuel_type(self):

print("Petrol")

def max_speed(self):

print("Max speed 350")

class BMW:

def fuel_type(self):

print("Diesel")

def max_speed(self):

print("Max speed is 240")

ferrari = Ferrari()

bmw = BMW()

# iterate objects of same type

for car in (ferrari, bmw):

# call methods without checking class of object

car.fuel_type()

car.max_speed()
 Run

Output
Petrol

Max speed 350


Diesel

Max speed is 240

As you can see, we have created two classes Ferrari and BMW. They have the same instance
method names fuel_type() and max_speed(). However, we have not linked both the classes nor
have we used inheritance.

We packed two different objects into a tuple and iterate through it using a car variable. It is possible
due to polymorphism because we have added the same method in both classes Python first checks
the object’s class type and executes the method present in its class.

Polymorphism with Function and Objects


We can create polymorphism with a function that can take any object as a parameter and execute its
method without checking its class type. Using this, we can call object actions using the same
function instead of repeating method calls.

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-polymorphism%2F 5/10
8/8/22, 8:17 PM Polymorphism in Python

Example
class Ferrari:

def fuel_type(self):

print("Petrol")

def max_speed(self):

print("Max speed 350")

class BMW:

def fuel_type(self):

print("Diesel")

def max_speed(self):

print("Max speed is 240")

# normal function

def car_details(obj):

obj.fuel_type()

obj.max_speed()

ferrari = Ferrari()

bmw = BMW()

car_details(ferrari)

car_details(bmw)

 Run

Output
Petrol

Max speed 350


Diesel

Max speed is 240

Polymorphism In Built-in Methods


The word polymorphism is taken from the Greek words poly (many) and morphism (forms). It
means a method can process objects differently depending on the class type or data type.

The built-in function reversed(obj) returns the iterable by reversing the given object. For example,
if you pass a string to it, it will reverse it. But if you pass a list of strings to it, it will return the
iterable by reversing the order of elements (it will not reverse the individual string).

Let us see how a built-in method process objects having different data types.

Example:
students = ['Emma', 'Jessa', 'Kelly']

school = 'ABC School'

print('Reverse string')

for i in reversed('PYnative'):

print(i, end=' ')

print('\nReverse list')

for i in reversed(['Emma', 'Jessa', 'Kelly']):

print(i, end=' ')


 Run

Output:
Reverse string

e v i t a n Y P

Reverse list

Kelly Jessa Emma

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-polymorphism%2F 6/10
8/8/22, 8:17 PM Polymorphism in Python

Method Overloading
The process of calling the same method with different parameters is known as method overloading.
Python does not support method overloading. Python considers only the latest defined method even
if you overload the method. Python will raise a TypeError if you overload the method.

Example
def addition(a, b):

c = a + b

print(c)

def addition(a, b, c):

d = a + b + c

print(d)

# the below line shows an error

# addition(4, 5)

# This line will call the second product method

addition(3, 7, 5)

 Run

To overcome the above problem, we can use different ways to achieve the method overloading. In
Python, to overload the class method, we need to write the method’s logic so that different code
executes inside the function depending on the parameter passes.

For example, the built-in function range() takes three parameters and produce different result
depending upon the number of parameters passed to it.

Example:
for i in range(5): print(i, end=', ')

print()

for i in range(5, 10): print(i, end=', ')

print()

for i in range(2, 12, 2): print(i, end=', ')


 Run

Output:
0, 1, 2, 3, 4,

5, 6, 7, 8, 9,

2, 4, 6, 8, 10,

Let’s assume we have an area() method to calculate the area of a square and rectangle. The method
will calculate the area depending upon the number of parameters passed to it.

If one parameter is passed, then the area of a square is calculated


If two parameters are passed, then the area of a rectangle is calculated.

Example: User-defined polymorphic method


class Shape:

# function with two default parameters

def area(self, a, b=0):

if b > 0:

print('Area of Rectangle is:', a * b)

else:

print('Area of Square is:', a ** 2)

square = Shape()

square.area(5)

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-polymorphism%2F 7/10
8/8/22, 8:17 PM Polymorphism in Python

rectangle = Shape()

rectangle.area(5, 3)

 Run

Output:
Area of Square is: 25

Area of Rectangle is: 15

Operator Overloading in Python


Operator overloading means changing the default behavior of an operator depending on the
operands (values) that we use. In other words, we can use the same operator for multiple purposes.

For example, the + operator will perform an arithmetic addition operation when used with
numbers. Likewise, it will perform concatenation when used with strings.

The operator + is used to carry out different operations for distinct data types. This is one of the
most simple occurrences of polymorphism in Python.

Example:
# add 2 numbers

print(100 + 200)

# concatenate two strings

print('Jess' + 'Roy')

# merger two list

print([10, 20, 30] + ['jessa', 'emma', 'kelly'])


 Run

Output:
300

JessRoy

[10, 20, 30, 'jessa', 'emma', 'kelly']

Overloading + operator for custom objects


Suppose we have two objects, and we want to add these two objects with a binary + operator.
However, it will throw an error if we perform addition because the compiler doesn’t add two
objects. See the following example for more details.

Example:
class Book:

def __init__(self, pages):

self.pages = pages

# creating two objects

b1 = Book(400)

b2 = Book(300)

# add two objects

print(b1 + b2)

 Run

Output
TypeError: unsupported operand type(s) for +: 'Book' and 'Book'

We can overload + operator to work with custom objects also. Python provides some special or
magic function that is automatically invoked when associated with that particular operator.

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-polymorphism%2F 8/10
8/8/22, 8:17 PM Polymorphism in Python

For example, when we use the + operator, the magic method __add__() is automatically invoked.
Internally + operator is implemented by using __add__() method. We have to override this method
in our class if you want to add two custom objects.

Example:
class Book:

def __init__(self, pages):

self.pages = pages

# Overloading + operator with magic method

def __add__(self, other):

return self.pages + other.pages

b1 = Book(400)

b2 = Book(300)

print("Total number of pages: ", b1 + b2)

 Run

Output
Total number of pages: 700

Overloading the * Operator


The * operator is used to perform the multiplication. Let’s see how to overload it to calculate the
salary of an employee for a specific period. Internally * operator is implemented by using the
__mul__() method.

Example:
class Employee:

def __init__(self, name, salary):

self.name = name

self.salary = salary

def __mul__(self, timesheet):

print('Worked for', timesheet.days, 'days')

# calculate salary

return self.salary * timesheet.days

class TimeSheet:

def __init__(self, name, days):

self.name = name

self.days = days

emp = Employee("Jessa", 800)

timesheet = TimeSheet("Jessa", 50)

print("salary is: ", emp * timesheet)

 Run

Output
Wroked for 50 days

salary is: 40000

Magic Methods
In Python, there are different magic methods available to perform overloading operations. The
below table shows the magic methods names to overload the mathematical operator, assignment
operator, and relational operators in Python.

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-polymorphism%2F 9/10
8/8/22, 8:17 PM Polymorphism in Python

Operator Name Symbol Magic method

Addition + __add__(self, other)

Subtraction - __sub__(self, other)

Multiplication * __mul__(self, other)

Division / __div__(self, other)

Floor Division // __floordiv__(self,other)

Modulus % __mod__(self, other)

Power ** __pow__(self, other)

Increment += __iadd__(self, other)

Decrement -= __isub__(self, other)

Product *= __imul__(self, other)

Division /+ __idiv__(self, other)

Modulus %= __imod__(self, other)

Power **= __ipow__(self, other)

Less than < __lt__(self, other)

Greater than > __gt__(self, other)

Less than or equal to <= __le__(self, other)

Greater than or equal to >= __ge__(self, other)

Equal to == __eq__(self, other)

Not equal != __ne__(self, other)

magic methods

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-polymorphism%2F 10/10
8/8/22, 8:18 PM Inheritance in Python

Inheritance in Python
The process of inheriting the properties of the parent class into a child class is called
inheritance. The existing class is called a base class or parent class and the new class is called a
subclass or child class or derived class.

In this Python lesson, you will learn inheritance, method overloading, method overriding, types of
inheritance, and MRO (Method Resolution Order).

In Object-oriented programming, inheritance is an important aspect. The main purpose of


inheritance is the reusability of code because we can use the existing class to create a new class
instead of creating it from scratch.

In inheritance, the child class acquires all the data members, properties, and functions from the
parent class. Also, a child class can also provide its specific implementation to the methods of the
parent class.

For example, In the real world, Car is a sub-class of a Vehicle class. We can create a Car by
inheriting the properties of a Vehicle such as Wheels, Colors, Fuel tank, engine, and add extra
properties in Car as required.

Syntax
class BaseClass:

Body of base class

class DerivedClass(BaseClass):

Body of derived class


 Run

Also, See

Python OOP Exercise

Types Of Inheritance
In Python, based upon the number of child and parent classes involved, there are five types of
inheritance. The type of inheritance are listed below:

1. Single inheritance
2. Multiple Inheritance
3. Multilevel inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance

Now let’s see each in detail with an example.

Single Inheritance
In single inheritance, a child class inherits from a single-parent class. Here is one child class and
one parent class.

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-inheritance%2F 1/9
8/8/22, 8:18 PM Inheritance in Python

Python Single Inheritance

Example

Let’s create one parent class called ClassOne and one child class called ClassTwo to implement
single inheritance.
# Base class

class Vehicle:

def Vehicle_info(self):

print('Inside Vehicle class')

# Child class
class Car(Vehicle):

def car_info(self):

print('Inside Car class')

# Create object of Car

car = Car()

# access Vehicle's info using car object

car.Vehicle_info()

car.car_info()

 Run

Output
Inside Vehicle class

Inside Car class

Multiple Inheritance
In multiple inheritance, one child class can inherit from multiple parent classes. So here is one
child class and multiple parent classes.

Python Multiple Inheritance

Example
# Parent class 1

class Person:

def person_info(self, name, age):

print('Inside Person class')

print('Name:', name, 'Age:', age)

# Parent class 2

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-inheritance%2F 2/9
8/8/22, 8:18 PM Inheritance in Python

class Company:

def company_info(self, company_name, location):

print('Inside Company class')

print('Name:', company_name, 'location:', location)

# Child class
class Employee(Person, Company):

def Employee_info(self, salary, skill):

print('Inside Employee class')

print('Salary:', salary, 'Skill:', skill)

# Create object of Employee

emp = Employee()

# access data
emp.person_info('Jessa', 28)

emp.company_info('Google', 'Atlanta')

emp.Employee_info(12000, 'Machine Learning')

 Run

Output
Inside Person class

Name: Jessa Age: 28

Inside Company class

Name: Google location: Atlanta

Inside Employee class

Salary: 12000 Skill: Machine Learning

In the above example, we created two parent classes Person and Company respectively. Then we
create one child called Employee which inherit from Person and Company classes.

Multilevel inheritance
In multilevel inheritance, a class inherits from a child class or derived class. Suppose three classes
A, B, C. A is the superclass, B is the child class of A, C is the child class of B. In other words, we
can say a chain of classes is called multilevel inheritance.

Python Multilevel Inheritance

Example
# Base class

class Vehicle:

def Vehicle_info(self):

print('Inside Vehicle class')

# Child class
class Car(Vehicle):

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-inheritance%2F 3/9
8/8/22, 8:18 PM Inheritance in Python

def car_info(self):

print('Inside Car class')

# Child class
class SportsCar(Car):

def sports_car_info(self):

print('Inside SportsCar class')

# Create object of SportsCar

s_car = SportsCar()

# access Vehicle's and Car info using SportsCar object

s_car.Vehicle_info()

s_car.car_info()

s_car.sports_car_info()

 Run

Output
Inside Vehicle class

Inside Car class

Inside SportsCar class

In the above example, we can see there are three classes named Vehicle, Car, SportsCar. Vehicle is
the superclass, Car is a child of Vehicle, SportsCar is a child of Car. So we can see the chaining of
classes.

Hierarchical Inheritance
In Hierarchical inheritance, more than one child class is derived from a single parent class. In other
words, we can say one parent class and multiple child classes.

Python hierarchical inheritance

Example

Let’s create ‘Vehicle’ as a parent class and two child class ‘Car’ and ‘Truck’ as a parent class.
class Vehicle:

def info(self):

print("This is Vehicle")

class Car(Vehicle):

def car_info(self, name):

print("Car name is:", name)

class Truck(Vehicle):

def truck_info(self, name):

print("Truck name is:", name)

obj1 = Car()

obj1.info()

obj1.car_info('BMW')

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-inheritance%2F 4/9
8/8/22, 8:18 PM Inheritance in Python

obj2 = Truck()

obj2.info()

obj2.truck_info('Ford')
 Run

Output
This is Vehicle

Car name is: BMW

This is Vehicle

Truck name is: Ford

Hybrid Inheritance
When inheritance is consists of multiple types or a combination of different inheritance is called
hybrid inheritance.

Python hybrid inheritance

Example
class Vehicle:

def vehicle_info(self):

print("Inside Vehicle class")

class Car(Vehicle):

def car_info(self):

print("Inside Car class")

class Truck(Vehicle):

def truck_info(self):

print("Inside Truck class")

# Sports Car can inherits properties of Vehicle and Car

class SportsCar(Car, Vehicle):

def sports_car_info(self):

print("Inside SportsCar class")

# create object

s_car = SportsCar()

s_car.vehicle_info()

s_car.car_info()

s_car.sports_car_info()
 Run

Note: In the above example, hierarchical and multiple inheritance exists. Here we created, parent
class Vehicle and two child classes named Car and Truck this is hierarchical inheritance.
read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-inheritance%2F 5/9
8/8/22, 8:18 PM Inheritance in Python

Another is SportsCar inherit from two parent classes named Car and Vehicle. This is multiple
inheritance.

Python super() function


When a class inherits all properties and behavior from the parent class is called inheritance. In such
a case, the inherited class is a subclass and the latter class is the parent class.

In child class, we can refer to parent class by using the super() function. The super function returns
a temporary object of the parent class that allows us to call a parent class method inside a child
class method.

Benefits of using the super() function.

1. We are not required to remember or specify the parent class name to access its
methods.
2. We can use the super() function in both single and multiple inheritances.
3. The super() function support code reusability as there is no need to write the entire
function

Example
class Company:

def company_name(self):

return 'Google'

class Employee(Company):

def info(self):

# Calling the superclass method using super()function

c_name = super().company_name()

print("Jessa works at", c_name)

# Creating object of child class

emp = Employee()

emp.info()
 Run

Output:
Jessa works at Google

In the above example, we create a parent class Company and child class Employee. In Employee class,
we call the parent class method by using a super() function.

issubclass() 
In Python, we can verify whether a particular class is a subclass of another class. For this purpose,
we can use Python built-in function issubclass(). This function returns True if the given class is
the subclass of the specified class. Otherwise, it returns False.

Syntax
issubclass(class, classinfo)
 Run

Where,

class: class to be checked.


classinfo: a class, type, or a tuple of classes or data types.

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-inheritance%2F 6/9
8/8/22, 8:18 PM Inheritance in Python

Example
class Company:

def fun1(self):

print("Inside parent class")

class Employee(Company):

def fun2(self):

print("Inside child class.")

class Player:

def fun3(self):

print("Inside Player class.")

# Result True
print(issubclass(Employee, Company))

# Result False

print(issubclass(Employee, list))

# Result False

print(issubclass(Player, Company))

# Result True
print(issubclass(Employee, (list, Company)))

# Result True
print(issubclass(Company, (list, Company)))
 Run

Also, see Python isinstance().

Method Overriding
In inheritance, all members available in the parent class are by default available in the child class.
If the child class does not satisfy with parent class implementation, then the child class is allowed
to redefine that method by extending additional functions in the child class. This concept is called
method overriding.

When a child class method has the same name, same parameters, and same return type as a method
in its superclass, then the method in the child is said to override the method in the parent class.

Python method overriding

Example
class Vehicle:

def max_speed(self):

print("max speed is 100 Km/Hour")

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-inheritance%2F 7/9
8/8/22, 8:18 PM Inheritance in Python

class Car(Vehicle):

# overridden the implementation of Vehicle class

def max_speed(self):

print("max speed is 200 Km/Hour")

# Creating object of Car class

car = Car()

car.max_speed()
 Run

Output:
max speed is 200 Km/Hour

In the above example, we create two classes named Vehicle (Parent class) and Car (Child class).
The class Car extends from the class Vehicle so, all properties of the parent class are available in
the child class. In addition to that, the child class redefined the method max_speed().

Method Resolution Order in Python


In Python, Method Resolution Order(MRO) is the order by which Python looks for a method or
attribute. First, the method or attribute is searched within a class, and then it follows the order we
specified while inheriting.

This order is also called the Linearization of a class, and a set of rules is called MRO (Method
Resolution Order). The MRO plays an essential role in multiple inheritances as a single
method may found in multiple parent classes.

In multiple inheritance, the following search order is followed.

1. First, it searches in the current parent class if not available, then searches in the parents
class specified while inheriting (that is left to right.)
2. We can get the MRO of a class. For this purpose, we can use either the mro attribute or
the mro() method.

Example
class A:

def process(self):

print(" In class A")

class B(A):

def process(self):

print(" In class B")

class C(B, A):

def process(self):

print(" In class C")

# Creating object of C class

C1 = C()

C1.process()

print(C.mro())

# In class C

# [<class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>]

In the above example, we create three classes named A, B and C. Class B is inherited from A, class C
inherits from B and A. When we create an object of the C class and calling the process() method,
Python looks for the process() method in the current class in the C class itself.

Then search for parent classes, namely B and A, because C class inherit from B and A. that is, C(B, A)
and always search in left to right manner.
read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-inheritance%2F 8/9
8/8/22, 8:18 PM Inheritance in Python

Next Steps
Python OOP Exercise

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-inheritance%2F 9/9
8/8/22, 8:19 PM Python Instance Variables Explained With Examples

Python Instance Variables Explained With


Examples
There are several kinds of variables in Python:

Instance variables in a class: these are called fields or attributes of an object


Local Variables: Variables in a method or block of code
Parameters: Variables in method declarations
Class variables: This variable is shared between all objects of a class

In Object-oriented programming, when we design a class, we use instance variables and class
variables.

Instance variables: If the value of a variable varies from object to object, then such
variables are called instance variables.
Class Variables: A class variable is a variable that is declared inside of class, but
outside of any instance method or __init__() method.

After reading this article, you’ll learn:

How to create and access instance variables


Modify values of instance variables
How to dynamically add or delete instance variables
Scope of a instance variables

What is an Instance Variable in Python?


If the value of a variable varies from object to object, then such variables are called instance
variables. For every object, a separate copy of the instance variable will be created.

Instance variables are not shared by objects. Every object has its own copy of the instance attribute.
This means that for each object of a class, the instance variable value is different.

When we create classes in Python, instance methods are used regularly. we need to create an object
to execute the block of code or action defined in the instance method.

Instance variables are used within the instance method. We use the instance method to perform a
set of actions on the data/value provided by the instance variable.

We can access the instance variable using the object and dot (.) operator.

In Python, to work with an instance variable and method, we use the self keyword. We use
the self keyword as the first parameter to a method. The self refers to the current object.

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-instance-variables%2F 1/8
8/8/22, 8:19 PM Python Instance Variables Explained With Examples

Declare Instance variable

Create Instance Variables


Instance variables are declared inside a method using the self keyword. We use a constructor to
define and initialize the instance variables. Let’s see the example to declare an instance variable in
Python.

Example:

In the following example, we are creating two instance variable name and age in the Student class.
class Student:

# constructor

def __init__(self, name, age):

# Instance variable

self.name = name

self.age = age

# create first object

s1 = Student("Jessa", 20)

# access instance variable

print('Object 1')

print('Name:', s1.name)

print('Age:', s1.age)

# create second object

s2= Student("Kelly", 10)

# access instance variable

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-instance-variables%2F 2/8
8/8/22, 8:19 PM Python Instance Variables Explained With Examples

print('Object 2')

print('Name:', s2.name)

print('Age:', s2.age)

 Run

Output
Object 1

Name: Jessa

Age: 20

Object 2

Name: Kelly

Age: 10

Note:

When we created an object, we passed the values to the instance variables using a
constructor.
Each object contains different values because we passed different values to a
constructor to initialize the object.
Variable declared outside __init__() belong to the class. They’re shared by all
instances.

Modify Values of Instance Variables


We can modify the value of the instance variable and assign a new value to it using the object
reference.

Note: When you change the instance variable’s values of one object, the changes will not be
reflected in the remaining objects because every object maintains a separate copy of the instance
variable.

Example
class Student:

# constructor

def __init__(self, name, age):

# Instance variable

self.name = name

self.age = age

# create object

stud = Student("Jessa", 20)

print('Before')

print('Name:', stud.name, 'Age:', stud.age)

# modify instance variable

stud.name = 'Emma'

stud.age = 15

print('After')

print('Name:', stud.name, 'Age:', stud.age)


 Run

Output
Before

Name: Jessa Age: 20

After

Name: Emma Age: 15

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-instance-variables%2F 3/8
8/8/22, 8:19 PM Python Instance Variables Explained With Examples

Ways to Access Instance Variable


There are two ways to access the instance variable of class:

Within the class in instance method by using the object reference (self)
Using getattr() method

Example 1: Access instance variable in the instance method


class Student:

# constructor

def __init__(self, name, age):

# Instance variable

self.name = name

self.age = age

# instance method access instance variable

def show(self):

print('Name:', stud.name, 'Age:', stud.age)

# create object

stud = Student("Jessa", 20)

# call instance method

stud.show()

 Run

Output
Name: Jessa Age: 20

instance variables and methods

Example 2: Access instance variable using getattr()


getattr(Object, 'instance_variable')
 Run

Pass the object reference and instance variable name to the getattr() method to get the value of an
instance variable.
class Student:

# constructor

def __init__(self, name, age):

# Instance variable

self.name = name

self.age = age

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-instance-variables%2F 4/8
8/8/22, 8:19 PM Python Instance Variables Explained With Examples

# create object

stud = Student("Jessa", 20)

# Use getattr instead of stud.name

print('Name:', getattr(stud, 'name'))

print('Age:', getattr(stud, 'age'))


 Run

Output
Name: Jessa

Age: 20

Instance Variables Naming Conventions


Instance variable names should be all lower case. For example, id
Words in an instance variable name should be separated by an underscore. For example,
store_name
Non-public instance variables should begin with a single underscore
If an instance name needs to be mangled, two underscores may begin its name

Dynamically Add Instance Variable to a Object


We can add instance variables from the outside of class to a particular object. Use the following
syntax to add the new instance variable to the object.
object_referance.variable_name = value
 Run

Example:
class Student:

def __init__(self, name, age):

# Instance variable

self.name = name

self.age = age

# create object

stud = Student("Jessa", 20)

print('Before')

print('Name:', stud.name, 'Age:', stud.age)

# add new instance variable 'marks' to stud

stud.marks = 75

print('After')

print('Name:', stud.name, 'Age:', stud.age, 'Marks:', stud.marks)

 Run

Output
Before

Name: Jessa Age: 20

After

Name: Jessa Age: 20 Marks: 75

Note:

We cannot add an instance variable to a class from outside because instance variables
belong to objects.
Adding an instance variable to one object will not be reflected the remaining
objects because every object has a separate copy of the instance variable.
read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-instance-variables%2F 5/8
8/8/22, 8:19 PM Python Instance Variables Explained With Examples

Dynamically Delete Instance Variable


In Python, we use the del statement and delattr() function to delete the attribute of an object.
Both of them do the same thing.

del statement: The del keyword is used to delete objects. In Python, everything is an
object, so the del keyword can also be used to delete variables, lists, or parts of a list,
etc.
delattr() function: Used to delete an instance variable dynamically.

Note: When we try to access the deleted attribute, it raises an attribute error.

Example 1: Using the del statement


class Student:

def __init__(self, roll_no, name):

# Instance variable

self.roll_no = roll_no

self.name = name

# create object

s1 = Student(10, 'Jessa')

print(s1.roll_no, s1.name)

# del name

del s1.name

# Try to access name variable

print(s1.name)

 Run

Output
10 Jessa

AttributeError: 'Student' object has no attribute 'name'

delattr() function

The delattr() function is used to delete the named attribute from the object with the prior
permission of the object. Use the following syntax.
delattr(object, name)

object: the object whose attribute we want to delete.


name: the name of the instance variable we want to delete from the object.

Example
class Student:

def __init__(self, roll_no, name):

# Instance variable

self.roll_no = roll_no

self.name = name

def show(self):

print(self.roll_no, self.name)

s1 = Student(10, 'Jessa')

s1.show()

# delete instance variable using delattr()

delattr(s1, 'roll_no')

s1.show()

 Run

Output
10 Jessa

AttributeError: 'Student' object has no attribute 'roll_no'


read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-instance-variables%2F 6/8
8/8/22, 8:19 PM Python Instance Variables Explained With Examples

Access Instance Variable From Another Class


We can access instance variables of one class from another class using object reference. It is useful
when we implement the concept of inheritance in Python, and we want to access the parent class
instance variable from a child class.

let’s understand this with the help of an example.

In this example, the engine is an instance variable of the Vehicle class. We inherited a Vehicle
class to access its instance variables in Car class
class Vehicle:

def __init__(self):

self.engine = '1500cc'

class Car(Vehicle):

def __init__(self, max_speed):

# call parent class constructor

super().__init__()

self.max_speed = max_speed

def display(self):

# access parent class instance variables 'engine'

print("Engine:", self.engine)

print("Max Speed:", self.max_speed)

# Object of car

car = Car(240)

car.display()

 Run

Output
Engine: 1500cc

Max Speed: 240

List all Instance Variables of a Object


We can get the list of all the instance variables the object has. Use the __dict__ function of an
object to get all instance variables along with their value.

The __dict__ function returns a dictionary that contains variable name as a key and variable value
as a value

Example:
class Student:

def __init__(self, roll_no, name):

# Instance variable

self.roll_no = roll_no

self.name = name

s1 = Student(10, 'Jessa')

print('Instance variable object has')

print(s1.__dict__)

# Get each instance variable

for key_value in s1.__dict__.items():

print(key_value[0], '=', key_value[1])

 Run

Output:
Instance variable object has

{'roll_no': 10, 'name': 'Jessa'}

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-instance-variables%2F 7/8
8/8/22, 8:19 PM Python Instance Variables Explained With Examples

roll_no = 10

name = Jessa

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-instance-variables%2F 8/8
8/8/22, 8:19 PM Python Instance Methods Explained With Examples

Python Instance Methods Explained With


Examples
In Python object-oriented programming, when we design a class, we use the instance methods and
class methods.

Inside a Class, we can define the following two types of methods.

Instance methods: Used to access or modify the object state. If we use instance
variables inside a method, such methods are called instance methods. It must have a
self parameter to refer to the current object.
Class methods: Used to access or modify the class state. In method implementation, if
we use only class variables, then such type of methods we should declare as a class
method. The class method has a cls parameter which refers to the class.

Also, read Python Class method vs Static method vs Instance method.

After reading this article, you’ll learn:

How to create and call instance methods


how to dynamically add or delete instance methods in Python

What is Instance Methods in Python


If we use instance variables inside a method, such methods are called instance methods. The
instance method performs a set of actions on the data/value provided by the instance
variables.

A instance method is bound to the object of the class.


It can access or modify the object state by changing the value of a instance variables

When we create a class in Python, instance methods are used regularly. To work with an instance
method, we use the self keyword. We use the self keyword as the first parameter to a method. The
self refers to the current object.

Any method we create in a class will automatically be created as an instance method unless we
explicitly tell Python that it is a class or static method.

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-instance-methods%2F 1/6
8/8/22, 8:19 PM Python Instance Methods Explained With Examples

Instance method in Python

Define Instance Method


Instance variables are not shared between objects. Instead, every object has its copy of the instance
attribute. Using the instance method, we can access or modify the calling object’s attributes.

Instance methods are defined inside a class, and it is pretty similar to defining a regular function.

Use the def keyword to define an instance method in Python.


Use self as the first parameter in the instance method when defining it. The self
parameter refers to the current object.
Using the self parameter to access or modify the current object attributes.

You may use a variable named differently for self, but it is discouraged since self is the
recommended convention in Python.

Let’s see the example to create an instance method show() in the Student class to display the student
details.

Example:
class Student:

# constructor

def __init__(self, name, age):

# Instance variable

self.name = name

self.age = age

# instance method to access instance variable

def show(self):

print('Name:', self.name, 'Age:', self.age)


 Run

Calling An Instance Method


We use an object and dot (.) operator to execute the block of code or action defined in the instance
method.
read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-instance-methods%2F 2/6
8/8/22, 8:19 PM Python Instance Methods Explained With Examples

First, create instance variables name and age in the Student class.
Next, create an instance method display() to print student name and age.
Next, create object of a Student class to call the instance method.

et’s see how to call an instance method show() to access the student object details such as name and
age.
class Student:

# constructor

def __init__(self, name, age):

# Instance variable

self.name = name

self.age = age

# instance method access instance variable

def show(self):

print('Name:', self.name, 'Age:', self.age)

# create first object

print('First Student')

emma = Student("Jessa", 14)

# call instance method

emma.show()

# create second object

print('Second Student')

kelly = Student("Kelly", 16)

# call instance method

kelly.show()
 Run

Output:
First Student
Name: Jessa Age: 14

Second Student

Name: Kelly Age: 16

Note:

Inside any instance method, we can use self to access any data or method that reside in our class.
We are unable to access it without a self parameter.

An instance method can freely access attributes and even modify the value of attributes of an object
by using the self parameter.

By Using self.__class__ attribute we can access the class attributes and change the class state.
Therefore instance method gives us control of changing the object as well as the class state.

Modify Instance Variables inside Instance Method


Let’s create the instance method update() method to modify the student age and roll number when
student data details change.
class Student:

def __init__(self, roll_no, name, age):

# Instance variable

self.roll_no = roll_no

self.name = name

self.age = age

# instance method access instance variable

def show(self):

print('Roll Number:', self.roll_no, 'Name:', self.name, 'Age:', self.age)

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-instance-methods%2F 3/6
8/8/22, 8:19 PM Python Instance Methods Explained With Examples
# instance method to modify instance variable

def update(self, roll_number, age):

self.roll_no = roll_number

self.age = age

# create object

print('class VIII')

stud = Student(20, "Emma", 14)

# call instance method

stud.show()

# Modify instance variables

print('class IX')

stud.update(35, 15)

stud.show()
 Run

Output:
class VIII

Roll Number: 20 Name: Emma Age: 14

class IX

Roll Number: 35 Name: Emma Age: 15

Create Instance Variables in Instance Method


Till the time we used constructor to create instance attributes. But, instance attributes are not
specific only to the __init__() method; they can be defined elsewhere in the class. So, let’s see
how to create an instance variable inside the method.

Example:
class Student:

def __init__(self, roll_no, name, age):

# Instance variable

self.roll_no = roll_no

self.name = name

self.age = age

# instance method to add instance variable

def add_marks(self, marks):

# add new attribute to current object

self.marks = marks

# create object

stud = Student(20, "Emma", 14)

# call instance method

stud.add_marks(75)

# display object

print('Roll Number:', stud.roll_no, 'Name:', stud.name, 'Age:', stud.age, 'Marks:', stud.marks)


 Run

Output:
Roll Number: 20 Name: Emma Age: 14 Marks: 75

Dynamically Add Instance Method to a Object


Usually, we add methods to a class body when defining a class. However, Python is a dynamic
language that allows us to add or delete instance methods at runtime. Therefore, it is helpful in the
following scenarios.

When class is in a different file, and you don’t have access to modify the class structure

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-instance-methods%2F 4/6
8/8/22, 8:19 PM Python Instance Methods Explained With Examples

You wanted to extend the class functionality without changing its basic structure
because many systems use the same structure.

Let’s see how to add an instance method in the Student class at runtime.

Example:

We should add a method to the object, so other instances don’t have access to that method. We use
the types module’s MethodType() to add a method to an object. Below is the simplest way to method
to an object.
import types

class Student:

# constructor

def __init__(self, name, age):

self.name = name

self.age = age

# instance method

def show(self):

print('Name:', self.name, 'Age:', self.age)

# create new method

def welcome(self):

print("Hello", self.name, "Welcome to Class IX")

# create object

s1 = Student("Jessa", 15)

# Add instance method to object

s1.welcome = types.MethodType(welcome, s1)

s1.show()

# call newly added method

s1.welcome()
 Run

Output:
Name: Jessa Age: 15

Hello Jessa Welcome to Class IX

Dynamically Delete Instance Methods


We can dynamically delete the instance method from the class. In Python, there are two ways to
delete method:

By using the del operator
By using delattr() method

By using the del operator

The del operator removes the instance method added by class.

Example:

In this example, we will delete an instance method named percentage() from a Student class. If
you try to access it after removing it, you’ll get an Attribute Error.
class Student:

# constructor

def __init__(self, name, age):

self.name = name

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-instance-methods%2F 5/6
8/8/22, 8:19 PM Python Instance Methods Explained With Examples
self.age = age

# instance method

def show(self):

print('Name:', self.name, 'Age:', self.age)

# instance method

def percentage(self, sub1, sub2):

print('Percentage:', (sub1 + sub2) / 2)

emma = Student('Emma', 14)

emma.show()

emma.percentage(67, 62)

# Delete the method from class using del operator

del emma.percentage

# Again calling percentage() method

# It will raise error

emma.percentage(67, 62)

 Run

Output:
Name: Emma Age: 14

Percentage: 64.5

File "/demos/oop/delete_method.py", line 21, in <module>

del emma.percentage

AttributeError: percentage

By using the delattr() method

The delattr() is used to delete the named attribute from the object with the prior permission of the
object. Use the following syntax to delete the instance method.
delattr(object, name)
 Run

object: the object whose attribute we want to delete.


name: the name of the instance method you want to delete from the object.

Example:

In this example, we will delete an instance method named percentage() from a Student class.
emma = Student('Emma', 14)

emma.show()

emma.percentage(67, 62)

# delete instance method percentage() using delattr()

delattr(emma, 'percentage')

emma.show()

# Again calling percentage() method

# It will raise error

emma.percentage(67, 62)
 Run

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-instance-methods%2F 6/6
8/8/22, 8:20 PM Python Class Variables

Python Class Variables


In Object-oriented programming, when we design a class, we use instance variables and class
variables.

In Class, attributes can be defined into two parts:

Instance variables: If the value of a variable varies from object to object, then such
variables are called instance variables.
Class Variables: A class variable is a variable that is declared inside of class, but
outside of any instance method or __init__() method.

After reading this article, you’ll learn:

How to create and access class variables


Modify values of a class variables
Instance variable vs. class variables
Behaviour of a class variable in inheritance

What is an Class Variable in Python?


If the value of a variable is not varied from object to object, such types of variables are called
class variables or static variables.

Class variables are shared by all instances of a class. Unlike instance variable, the value of a class
variable is not varied from object to object,

In Python, Class variables are declared when a class is being constructed. They are not defined
inside any methods of a class because of this only one copy of the static variable will be created
and shared between all objects of the class.

For example, in Student class, we can have different instance variables such as name and roll
number because each student’s name and roll number are different.

But, if we want to include the school name in the student class, we must use the class variable
instead of an instance variable as the school name is the same for all students. So instead of
maintaining the separate copy in each object, we can create a class variable that will hold the
school name so all students (objects) can share it.

We can add any number of class variables in a class.

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-class-variables%2F 1/8
8/8/22, 8:20 PM Python Class Variables

Understand Class Variables

Create Class Variables


A class variable is declared inside of class, but outside of any instance method
or __init__() method.

By convention, typically it is placed right below the class header and before the constructor method
and other methods.

Example:
class Student:

# Class variable

school_name = 'ABC School '

def __init__(self, name, roll_no):

self.name = name

self.roll_no = roll_no

# create first object

s1 = Student('Emma', 10)

print(s1.name, s1.roll_no, Student.school_name)

# access class variable

# create second object

s2 = Student('Jessa', 20)

# access class variable

print(s2.name, s2.roll_no, Student.school_name)

 Run

Output
read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-class-variables%2F 2/8
8/8/22, 8:20 PM Python Class Variables

Emma 10 ABC School

Jessa 20 ABC School

In the above example, we created the class variable school_name and accessed it using the object
and class name.

Note: Like regular variables, class variables can store data of any type. We can use Python list,
Python tuple, and Python dictionary as a class variable.

Accessing Class Variables


We can access static variables either by class name or by object reference, but it is recommended to
use the class name.

In Python, we can access the class variable in the following places

Access inside the constructor by using either self parameter or class name.
Access class variable inside instance method by using either self of class name
Access from outside of class by using either object reference or class name.

Example 1: Access Class Variable in the constructor


class Student:

# Class variable

school_name = 'ABC School '

# constructor

def __init__(self, name):

self.name = name

# access class variable inside constructor using self

print(self.school_name)

# access using class name

print(Student.school_name)

# create Object

s1 = Student('Emma')
 Run

Output
ABC School

ABC School

Example 2: Access Class Variable in Instance method and outside class


class Student:

# Class variable

school_name = 'ABC School '

# constructor

def __init__(self, name, roll_no):

self.name = name

self.roll_no = roll_no

# Instance method

def show(self):

print('Inside instance method')

# access using self

print(self.name, self.roll_no, self.school_name)

# access using class name

print(Student.school_name)

# create Object

s1 = Student('Emma', 10)

s1.show()

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-class-variables%2F 3/8
8/8/22, 8:20 PM Python Class Variables

print('Outside class')

# access class variable outside class

# access using object reference

print(s1.school_name)

# access using class name

print(Student.school_name)

 Run

Output
Inside instance method

Emma 10 ABC School

ABC School

Outside class
ABC School

ABC School

In this example, we accessed the class variable school_name using class name and a self keyword
inside a method.

Modify Class Variables


Generally, we assign value to a class variable inside the class declaration. However, we can change
the value of the class variable either in the class or outside of class.

Note: We should change the class variable’s value using the class name only.

Example
class Student:

# Class variable

school_name = 'ABC School '

# constructor

def __init__(self, name, roll_no):

self.name = name

self.roll_no = roll_no

# Instance method

def show(self):

print(self.name, self.roll_no, Student.school_name)

# create Object

s1 = Student('Emma', 10)

print('Before')

s1.show()

# Modify class variable

Student.school_name = 'XYZ School'

print('After')

s1.show()

 Run

Output:
Before

Emma 10 ABC School

After

Emma 10 XYZ School

Note:

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-class-variables%2F 4/8
8/8/22, 8:20 PM Python Class Variables

It is best practice to use a class name to change the value of a class variable. Because if we try to
change the class variable’s value by using an object, a new instance variable is created for that
particular object, which shadows the class variables.

Example:
class Student:

# Class variable

school_name = 'ABC School '

# constructor

def __init__(self, name, roll_no):

self.name = name

self.roll_no = roll_no

# create Objects

s1 = Student('Emma', 10)

s2 = Student('Jessa', 20)

print('Before')

print(s1.name, s1.roll_no, s1.school_name)

print(s2.name, s2.roll_no, s2.school_name)

# Modify class variable using object reference

s1.school_name = 'PQR School'

print('After')

print(s1.name, s1.roll_no, s1.school_name)

print(s2.name, s2.roll_no, s2.school_name)

 Run

Output:
Before

Emma 10 ABC School

Jessa 20 ABC School

After

Emma 10 PQR School

Jessa 20 ABC School

A new instance variable is created for the s1 object, and this variable shadows the class variables.
So always use the class name to modify the class variable.

Class Variable vs Instance variables


The following table shows the difference between the instance variable and the class variable.

In Python, properties can be defined into two parts:

Instance variables: Instance variable’s value varies from object to object. Instance
variables are not shared by objects. Every object has its own copy of the instance
attribute
Class Variables: A class variable is a variable that is declared inside of class, but
outside of any instance method or __init__() method. Class variables are shared by all
instances of a class.

Read More: Instance variables in Python with Examples

Instance Variable Class Variable

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-class-variables%2F 5/8
8/8/22, 8:20 PM Python Class Variables

Instance Variable Class Variable

Instance variables are not shared by Class variables are shared by all instances.

objects. Every object has its own copy of


the instance attribute

Instance variables are declared inside the Class variables are declared inside the class
constructor i.e., the __init__() method. definition but outside any of the instance
methods and constructors.

It is gets created when an instance of the It is created when the program begins to execute.
class is created.

Changes made to these variables through Changes made in the class variable will reflect in
one object will not reflect in another all objects.
object.

Class Variables vs. Instance Variables

Example:

Let’s see the example to create a class variable and instance variable.
class Car:

# Class variable

manufacturer = 'BMW'

def __init__(self, model, price):

# instance variable

self.model = model

self.price = price

# create Object

car = Car('x1', 2500)

print(car.model, car.price, Car.manufacturer)


 Run

Output:
x1 2500 BMW

Class Variables In Inheritance


As you know, only one copy of the class variable will be created and shared between all objects of
that class.

When we use inheritance, all variables and methods of the base class are available to the child
class. In such cases, We can also change the value of the parent class’s class variable in the child
class.

We can use the parent class or child class name to change the value of a parent class’s class
variable in the child class.

Example
class Course:

# class variable

course = "Python"

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-class-variables%2F 6/8
8/8/22, 8:20 PM Python Class Variables

class Student(Course):

def __init__(self, name):

self.name = name

def show_student(self):

# Accessing class variable of parent class

print('Before')

print("Student name:", self.name, "Course Name:", Student.course)

# changing class variable value of base class

print('Now')

Student.course = "Machine Learning"

print("Student name:", self.name, "Course Name:", Student.course)

# creating object of Student class

stud = Student("Emma")

stud.show_student()
 Run

Output
Before

Student name: Emma Course Name: Python

Now

Student name: Emma Course Name: Machine Learning

What if both child class and parent class has the same class variable name. In this case, the
child class will not inherit the class variable of a base class. So it is recommended to create a
separate class variable for child class instead of inheriting the base class variable.

Example:
class Course:

# class variable

course = "Python"

class Student(Course):

# class variable

course = "SQL"

def __init__(self, name):

self.name = name

def show_student(self):

# Accessing class variable

print('Before')

print("Student name:", self.name, "Course Name:", Student.course)

# changing class variable's value

print('Now')

Student.course = "Machine Learning"

print("Student name:", self.name, "Course Name:", Student.course)

# creating object of Student class

stud = Student("Emma")

stud.show_student()

# parent class course name

print('Parent Class Course Name:', Course.course)


 Run

Output:
Before

Student name: Emma Course Name: SQL

Now

Student name: Emma Course Name: Machine Learning

Parent Class Course Name: Python

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-class-variables%2F 7/8
8/8/22, 8:20 PM Python Class Variables

Wrong Use of Class Variables


In Python, we should properly use the class variable because all objects share the same copy. Thus,
if one of the objects modifies the value of a class variable, then all objects start referring to the
fresh copy.

For example,

Example
class Player:

# class variables

club = 'Chelsea'
sport = 'Football'

def __init__(self, name):

# Instance variable

self.name = name

def show(self):

print("Player :", 'Name:', self.name, 'Club:', self.club, 'Sports:', self.sport)

p1 = Player('John')

# wrong use of class variable

p1.club = 'FC'

p1.show()

p2 = Player('Emma')

p2.sport = 'Tennis'

p2.show()

# actual class variable value

print('Club:', Player.club, 'Sport:', Player.sport)


 Run

Output
Player : Name: John Club: FC Sports: Football

Player : Name: Emma Club: Chelsea Sports: Tennis

Club: Chelsea Sport: Football

In the above example, the instance variable name is unique for each player. The class variable team
and sport can be accessed and modified by any object.

Because both objects modified the class variable, a new instance variable is created for that
particular object with the same name as the class variable, which shadows the class variables.

In our case, for object p1 new instance variable club gets created, and for object p2 new instance
variable sport gets created.

So when you try to access the class variable using the p1 or p2 object, it will not return the actual
class variable value.

To avoid this, always modify the class variable value using the class name so that all objects gets
the updated value. Like this
Player.club = 'FC'

Player.sport = 'Tennis'
 Run

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-class-variables%2F 8/8
8/8/22, 8:23 PM Python Class Method Explained With Examples

Python Class Method Explained With


Examples
In Object-oriented programming, we use instance methods and class methods. Inside a Class, we
can define the following three types of methods.

Instance method: Used to access or modify the object state. If we use instance
variables inside a method, such methods are called instance methods. It must have a
self parameter to refer to the current object.
Class method: Used to access or modify the class state. In method implementation, if
we use only class variables, then such type of methods we should declare as a class
method. The class method has a cls parameter which refers to the class.
Static method: It is a general utility method that performs a task in isolation. Inside this
method, we don’t use instance or class variable because this static method doesn’t take
any parameters like self and cls.

Also, read Python Class method vs Static method vs Instance method.

After reading this article, you’ll learn:

How to create and use the class methods in Python


Create class method using the @classmethod decorator and classmethod() function
how to dynamically add or delete class methods

What is Class Method in Python


Class methods are methods that are called on the class itself, not on a specific object instance.
Therefore, it belongs to a class level, and all class instances share a class method.

A class method is bound to the class and not the object of the class. It can access only
class variables.
It can modify the class state by changing the value of a class variable that would apply
across all the class objects.

In method implementation, if we use only class variables, we should declare such methods as class
methods. The class method has a cls as the first parameter, which refers to the class.

Class methods are used when we are dealing with factory methods. Factory methods are those
methods that return a class object for different use cases. Thus, factory methods create concrete
implementations of a common interface.

The class method can be called using ClassName.method_name() as well as by using an object of the
class.

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-class-method%2F 1/7
8/8/22, 8:23 PM Python Class Method Explained With Examples

Define class method

Define Class Method


Any method we create in a class will automatically be created as an instance method. We must
explicitly tell Python that it is a class method using the @classmethod decorator or classmethod()
function.

Class methods are defined inside a class, and it is pretty similar to defining a regular function.

Like, inside an instance method, we use the self keyword to access or modify the instance
variables. Same inside the class method, we use the cls keyword as a first parameter to access class
variables. Therefore the class method gives us control of changing the class state.

You may use a variable named differently for cls, but it is discouraged since self is the
recommended convention in Python.
The class method can only access the class attributes, not the instance attributes

Example 1: Create Class Method Using @classmethod Decorator


To make a method as class method, add @classmethod decorator before the method definition, and
add cls as the first parameter to the method.

The @classmethod decorator is a built-in function decorator. In Python, we use the @classmethod
decorator to declare a method as a class method. The @classmethod decorator is an expression that
gets evaluated after our function is defined.

Let’s see how to create a factory method using the class method. In this example, we will create a
Student class object using the class method.
from datetime import date

class Student:

def __init__(self, name, age):

self.name = name

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-class-method%2F 2/7
8/8/22, 8:23 PM Python Class Method Explained With Examples

self.age = age

@classmethod

def calculate_age(cls, name, birth_year):

# calculate age an set it as a age

# return new object

return cls(name, date.today().year - birth_year)

def show(self):

print(self.name + "'s age is: " + str(self.age))

jessa = Student('Jessa', 20)

jessa.show()

# create new object using the factory method

joy = Student.calculate_age("Joy", 1995)

joy.show()

 Run

Output
Jessa's age is: 20

John's age is: 26

In the above example, we created two objects, one using the constructor and the second
using the calculate_age() method.
The constructor takes two arguments name and age. On the other hand, class method
takes cls, name, and birth_year and returns a class instance which nothing but a new
object.
The @classmethod decorator is used for converting calculate_age() method to a class
method.
The calculate_age() method takes Student class (cls) as a first parameter and returns
constructor by calling Student(name, date.today().year - birthYear), which is
equivalent to Student(name, age).

Example 2: Create Class Method Using classmethod() function


Apart from a decorator, the built-in function classmethod() is used to convert a normal method into
a class method. The classmethod() is an inbuilt function in Python, which returns a class method
for a given function.

Syntax:
classmethod(function)
 Run

function: It is the name of the method you want to convert as a class method.
It returns the converted class method.

Note: The method you want to convert as a class method must accept class (cls) as the first
argument, just like an instance method receives the instance (self).

As we know, the class method is bound to class rather than an object. So we can call the class
method both by calling class and object.

A classmethod() function is the older way to create the class method in Python. In a newer version
of Python, we should use the @classmethod decorator to create a class method.

Example: Create class method using classmethod() function


class School:

# class variable

name = 'ABC School'

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-class-method%2F 3/7
8/8/22, 8:23 PM Python Class Method Explained With Examples

def school_name(cls):

print('School Name is :', cls.name)

# create class method

School.school_name = classmethod(School.school_name)

# call class method

School.school_name()
 Run

Output
School Name is : ABC School

Example 3: Access Class Variables in Class Methods


Using the class method, we can only access or modify the class variables. Let’s see how to access
and modify the class variables in the class method.

Class variables are shared by all instances of a class. Using the class method we can modify the
class state by changing the value of a class variable that would apply across all the class objects.
class Student:

school_name = 'ABC School'

def __init__(self, name, age):

self.name = name

self.age = age

@classmethod

def change_school(cls, school_name):

# class_name.class_variable

cls.school_name = school_name

# instance method

def show(self):

print(self.name, self.age, 'School:', Student.school_name)

jessa = Student('Jessa', 20)

jessa.show()

# change school_name

Student.change_school('XYZ School')

jessa.show()
 Run

Output:
Jessa 20 School: ABC School

Jessa 20 School: XYZ School

Class Method in Inheritance


In inheritance, the class method of a parent class is available to a child class.

Let’s create a Vehicle class that contains a factory class method from_price() that will return a
Vehicle instance from a price. When we call the same method using the child’s class name, it will
return the child’s class object.

Whenever we derive a class from a parent class that has a class method then it creates the correct
instance of the derived class. The following example shows how the class method works in
inheritance.

Example
read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-class-method%2F 4/7
8/8/22, 8:23 PM Python Class Method Explained With Examples

class Vehicle:

brand_name = 'BMW'

def __init__(self, name, price):

self.name = name

self.price = price

@classmethod

def from_price(cls, name, price):

# ind_price = dollar * 76

# create new Vehicle object

return cls(name, (price * 75))

def show(self):

print(self.name, self.price)

class Car(Vehicle):

def average(self, distance, fuel_used):

mileage = distance / fuel_used

print(self.name, 'Mileage', mileage)

bmw_us = Car('BMW X5', 65000)

bmw_us.show()

# class method of parent class is available to child class

# this will return the object of calling class

bmw_ind = Car.from_price('BMW X5', 65000)

bmw_ind.show()

# check type

print(type(bmw_ind))
 Run

Output
BMW X5 65000

BMW X5 4875000

class '__main__.Car'

Dynamically Add Class Method to a Class


Typically, we add class methods to a class body when defining a class. However, Python is a
dynamic language that allows us to add or delete methods at runtime. Therefore, it is helpful when
you wanted to extend the class functionality without changing its basic structure because many
systems use the same structure.

We need to use the classmethod() function to add a new class method to a class.

Example:

Let’s see how to add a new class method in the Student class at runtime.
class Student:

school_name = 'ABC School'

def __init__(self, name, age):

self.name = name

self.age = age

def show(self):

print(self.name, self.age)

# class ended

# method outside class

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-class-method%2F 5/7
8/8/22, 8:23 PM Python Class Method Explained With Examples

def exercises(cls):

# access class variables

print("Below exercises for", cls.school_name)

# Adding class method at runtime to class

Student.exercises = classmethod(exercises)

jessa = Student("Jessa", 14)

jessa.show()

# call the new method

Student.exercises()

 Run

Output
Jessa 14

Below exercises for ABC School

Dynamically Delete Class Methods


We can dynamically delete the class methods from the class. In Python, there are two ways to do it:

By using the del operator
By using delattr() method

By using the del operator

The del operator removes the instance method added by class. Use the del
class_name.class_method syntax to delete the class method.

Example:

In this example, we will delete the class method named change_school() from a Student class. If
you try to access it after removing it, you’ll get an Attribute Error.
class Student:

school_name = 'ABC School'

def __init__(self, name, age):

self.name = name

self.age = age

@classmethod

def change_school(cls, school_name):

cls.school_name = school_name

jessa = Student('Jessa', 20)

print(Student.change_school('XYZ School'))

print(Student.school_name)

# delete class method

del Student.change_school

# call class method

# it will give error

print(Student.change_school('PQR School'))

 Run

Output
XYZ School

AttributeError: type object 'Student' has no attribute 'change_school'

By using delatt() method

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-class-method%2F 6/7
8/8/22, 8:23 PM Python Class Method Explained With Examples

The delattr() method is used to delete the named attribute and method from the class. The
argument to delattr is an object and string. The string must be the name of an attribute or method
name.

Example
jessa = Student('Jessa', 20)

print(Student.change_school('XYZ School'))

print(Student.school_name)

# delete class method

delattr(Student, 'change_school')

# call class method

# it will give error

print(Student.change_school('PQR School'))

 Run

Output
XYZ School

AttributeError: type object 'Student' has no attribute 'change_school'

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-class-method%2F 7/7
8/8/22, 8:24 PM Python Static Method Explained With Examples

Python Static Method Explained With


Examples
In Object-oriented programming, at the class level, we use class methods and static methods.

Class methods: Used to access or modify the state of the class. if we use only class
variables, we should declare such methods as a class method.
Static methods: A static method is a general utility method that performs a task in
isolation. Inside this method, we don’t use instance or class variable because this static
method doesn’t take any parameters like self and cls.

Also, read Python Class method vs Static method vs Instance method.

After reading this article, you’ll learn:

How to create and use the static methods in Python


Create staticmethod using the @staticmethod decorator and staticmethod() function

What is Static Methods in Python


A static method is a general utility method that performs a task in isolation. Static methods in
Python are similar to those found in Java or C++.

A static method is bound to the class and not the object of the class. Therefore, we can call it using
the class name.

A static method doesn’t have access to the class and instance variables because it does not receive
an implicit first argument like self and cls. Therefore it cannot modify the state of the object or
class.

The class method can be called using ClassName.method_name() as well as by using an object of the
class.
class Employee:

@staticmethod

def sample(x):

print('Inside static method', x)

# call static method

Employee.sample(10)

# can be called using object

emp = Employee()

emp.sample(10)
 Run

Define Static Method in Python


Any method we create in a class will automatically be created as an instance method. We must
explicitly tell Python that it is a static method using the @staticmethod decorator
or staticmethod() function.

Static methods are defined inside a class, and it is pretty similar to defining a regular function. To
declare a static method, use this idiom:
read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-static-method%2F 1/4
8/8/22, 8:24 PM Python Static Method Explained With Examples

class C:

@staticmethod

def f(arg1, arg2, ...): ...


 Run

Example: Create Static Method Using @staticmethod Decorator


To make a method a static method, add @staticmethod decorator before the method definition.

The @staticmethod decorator is a built-in function decorator in Python to declare a method as a


static method. It is an expression that gets evaluated after our function is defined.

In this example, we will create a static method gather_requirement() that accepts the project name
and returns all requirements to complete under this project.

Static methods are a special case of methods. Sometimes, you’ll write code that belongs to a class,
but that doesn’t use the object itself at all. It is a utility method and doesn’t need an object (self
parameter) to complete its operation. So we declare it as a static method. Also, we can call it from
another method of a class.
class Employee(object):

def __init__(self, name, salary, project_name):

self.name = name

self.salary = salary

self.project_name = project_name

@staticmethod

def gather_requirement(project_name):

if project_name == 'ABC Project':

requirement = ['task_1', 'task_2', 'task_3']

else:

requirement = ['task_1']

return requirement

# instance method

def work(self):

# call static method from instance method

requirement = self.gather_requirement(self.project_name)

for task in requirement:

print('Completed', task)

emp = Employee('Kelly', 12000, 'ABC Project')

emp.work()

 Run

Output:
Completed task_1

Completed task_2

Completed task_3

Advantages of a Static Method


Here, the static method has the following advantages

Consume Less memory: Instance methods are object too, and creating them has a cost.
Having a static method avoids that. Let’s assume you have ten employee objects and if
you create gather_requirement() as a instance method then Python have to create a ten
copies of this method (seperate for each object) which will consume more memeory. On
the other hand static method has only one copy per class.

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-static-method%2F 2/4
8/8/22, 8:24 PM Python Static Method Explained With Examples

kelly = Employee('Kelly', 12000, 'ABC Project')

jessa = Employee('Jessa', 7000, 'XYZ Project')

# false

# because seperate copy of instance method is created for each object

print(kelly.work is jessa.work)

# True

# because only one copy is created

# kelly and jess objects share the same methods

print(kelly.gather_requirement is jessa.gather_requirement)

# True

print(kelly.gather_requirement is Employee.gather_requirement)
 Run

To Write Utility functions: Static methods have limited use because they don’t have
access to the attributes of an object (instance variables) and class attributes (class
variables). However, they can be helpful in utility such as conversion form one type to
another. The parameters provided are enough to operate.
Readabiltity: Seeing the @staticmethod at the top of the method, we know that the
method does not depend on the object’s state or the class state.

The staticmethod() function


Some code might use the old method of defining a static method, using staticmethod() as a
function rather than a decorator.

You should only use staticmethod() function to define static method if you have to support older
versions of Python (2.2 and 2.3). Otherwise, it is recommended to use the @staticmethod decorator.

Syntax:
staticmethod(function)
 Run

function: It is the name of the method you want to convert as a static method.
It returns the converted static method.

Example:
class Employee:

def sample(x):

print('Inside static method', x)

# convert to static method

Employee.sample = staticmethod(Employee.sample)

# call static method

Employee.sample(10)
 Run

The staticmethod() approach is helpful when you need a reference to a function from a class body
and you want to avoid the automatic transformation to the instance method.

Call Static Method from Another Method


Let’s see how to call a static method from another static method of the same class. Here we will
class a static method from a class method.
class Test :

@staticmethod

def static_method_1():

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-static-method%2F 3/4
8/8/22, 8:24 PM Python Static Method Explained With Examples

print('static method 1')

@staticmethod

def static_method_2() :

Test.static_method_1()

@classmethod

def class_method_1(cls) :

cls.static_method_2()

# call class method

Test.class_method_1()
 Run

Output:
static method 1

Referance: Static method documentation

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-static-method%2F 4/4
8/8/22, 8:25 PM Python Class Method vs. Static Method vs. Instance Method

Python Class Method vs. Static Method vs.


Instance Method
In this tutorial, you’ll understand the difference between class method vs. static method vs. instance
method step by step.

In Object-oriented programming, when we design a class, we use the following three methods

Instance method performs a set of actions on the data/value provided by the instance
variables. If we use instance variables inside a method, such methods are called instance
methods.
Class method is method that is called on the class itself, not on a specific object
instance. Therefore, it belongs to a class level, and all class instances share a class
method.
Static method is a general utility method that performs a task in isolation. This method
doesn’t have access to the instance and class variable.

class method vs static method vs instance method

Difference #1: Primary Use


Class method Used to access or modify the class state. It can modify the class state by
changing the value of a class variable that would apply across all the class objects.
The instance method acts on an object’s attributes. It can modify the object state by
changing the value of instance variables.
Static methods have limited use because they don’t have access to the attributes of an
object (instance variables) and class attributes (class variables). However, they can be
helpful in utility such as conversion form one type to another.

Class methods are used as a factory method. Factory methods are those methods that return a class
object for different use cases. For example, you need to do some pre-processing on the provided
data before creating an object.

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-class-method-vs-static-method-vs-instance-method%2F 1/5
8/8/22, 8:25 PM Python Class Method vs. Static Method vs. Instance Method

Read our separate tutorial on

Difference #2: Method Defination


Let’s learn how to define instance method, class method, and static method in a class. All three
methods are defined in different ways.

All three methods are defined inside a class, and it is pretty similar to defining a regular
function.
Any method we create in a class will automatically be created as an instance method.
We must explicitly tell Python that it is a class method or static method.
Use the @classmethod decorator or the classmethod() function to define the class
method
Use the @staticmethod decorator or the staticmethod() function to define a static
method.

Example:

Use self as the first parameter in the instance method when defining it. The self
parameter refers to the current object.
On the other hand, Use cls as the first parameter in the class method when defining it.
The cls refers to the class.
A static method doesn’t take instance or class as a parameter because they don’t have
access to the instance variables and class variables.

class Student:

# class variables

school_name = 'ABC School'

# constructor

def __init__(self, name, age):

# instance variables

self.name = name

self.age = age

# instance variables

def show(self):

print(self.name, self.age, Student.school_name)

@classmethod

def change_School(cls, name):

cls.school_name = name

@staticmethod

def find_notes(subject_name):

return ['chapter 1', 'chapter 2', 'chapter 3']


 Run

As you can see in the example, in the instance

Difference #3: Method Call


Class methods and static methods can be called using ClassName or by using a class
object.
The Instance method can be called only using the object of the class.

Example:

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-class-method-vs-static-method-vs-instance-method%2F 2/5
8/8/22, 8:25 PM Python Class Method vs. Static Method vs. Instance Method

# create object

jessa = Student('Jessa', 12)

# call instance method

jessa.show()

# call class method using the class

Student.change_School('XYZ School')

# call class method using the object

jessa.change_School('PQR School')

# call static method using the class

Student.find_notes('Math')

# call class method using the object

jessa.find_notes('Math')
 Run

Output:
Jessa 12 ABC School

School name changed to XYZ School

School name changed to PQR School

Difference #4: Attribute Access


Both class and object have attributes. Class attributes include class variables, and object attributes
include instance variables.

The instance method can access both class level and object attributes. Therefore, It can
modify the object state.
Class methods can only access class level attributes. Therefore, It can modify the class
state.
A static method doesn’t have access to the class attribute and instance attributes.
Therefore, it cannot modify the class or object state.

Example:
class Student:

# class variables

school_name = 'ABC School'

def __init__(self, name, age):

self.name = name

self.age = age

# instance method

def show(self):

# access instance variables

print('Student:', self.name, self.age)

# access class variables

print('School:', self.school_name)

@classmethod

def change_School(cls, name):

# access class variable

print('Previous School name:', cls.school_name)

cls.school_name = name

print('School name changed to', Student.school_name)

@staticmethod

def find_notes(subject_name):

# can't access instance or class attributes

return ['chapter 1', 'chapter 2', 'chapter 3']

# create object

jessa = Student('Jessa', 12)

# call instance method

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-class-method-vs-static-method-vs-instance-method%2F 3/5
8/8/22, 8:25 PM Python Class Method vs. Static Method vs. Instance Method

jessa.show()

# call class method

Student.change_School('XYZ School')
 Run

Output:
Student: Jessa 12

School: ABC School

Previous School name: ABC School

School name changed to XYZ School

Difference #5: Class Bound and Instance Bound


An instance method is bound to the object, so we can access them using the object of
the class.
Class methods and static methods are bound to the class. So we should access them
using the class name.

Example:
class Student:

def __init__(self, roll_no): self.roll_no = roll_no

# instance method

def show(self):

print('In Instance method')

@classmethod

def change_school(cls, name):

print('In class method')

@staticmethod

def find_notes(subject_name):

print('In Static method')

# create two objects

jessa = Student(12)

# instance method bound to object

print(jessa.show)

# class method bound to class

print(jessa.change_school)

# static method bound to class

print(jessa.find_notes)

 Run

Do you know:

In Python, a separate copy of the instance methods will be created for every object.

Suppose you create five Student objects, then Python has to create five copies of the show() method
(separate for each object). So it will consume more memory. On the other hand, the static method
has only one copy per class.

Example:
# create two objects

jessa = Student(12)

kelly = Student(25)

# False because two separate copies

print(jessa.show is kelly.show)

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-class-method-vs-static-method-vs-instance-method%2F 4/5
8/8/22, 8:25 PM Python Class Method vs. Static Method vs. Instance Method

# True objects share same copies of static methods

print(jessa.find_notes is kelly.find_notes)
 Run
Jessa 20 ABC School

Jessa 20 XYZ School

<bound method Student.change_School of <class '__main__.Student'>>

As you can see in the output, the change_School() method is bound to the class.

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-class-method-vs-static-method-vs-instance-method%2F 5/5
8/8/22, 8:26 PM Python Object-Oriented Programming (OOP) Exercise: Classes and Objects Exercises

Python Object-Oriented Programming (OOP)


Exercise: Classes and Objects Exercises
This Object-Oriented Programming (OOP) exercise aims to help you to learn and practice OOP
concepts. All questions are tested on Python 3.

Python Object-oriented programming (OOP) is based on the concept of “objects,” which can
contain data and code: data in the form of instance variables (often known as attributes or
properties), and code, in the form method. I.e., Using OOP, we encapsulate related properties and
behaviors into individual objects.

What is included in this Python OOP exercise?

This OOP classes and objects exercise includes 8 different programs, questions, and challenges. All
solutions are tested on Python 3.

This OOP exercise covers questions on the following topics:

Class and Object creation


Instance variables and Methods, and Class level attributes
Model systems with class inheritance i.e., inherit From Other Classes
Parent Classes and Child Classes
Extend the functionality of Parent Classes using Child class
Object checking

When you complete each question, you get more familiar with the Python OOP. Let us know if you
have any alternative solutions. It will help other developers.

Use Online Code Editor to solve exercise questions.

OOP Exercise 1: Create a Class with instance attributes


Write a Python program to create a Vehicle class with max_speed and mileage instance attributes.

Refer:
Show Solution

OOP Exercise 2: Create a Vehicle class without any variables and methods
Show Solution

OOP Exercise 3: Create a child class Bus that will inherit all of the variables
and methods of the Vehicle class
Given:
class Vehicle:

def __init__(self, name, max_speed, mileage):

self.name = name

self.max_speed = max_speed

self.mileage = mileage
 Run
read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-object-oriented-programming-oop-exercise%2F 1/4
8/8/22, 8:26 PM Python Object-Oriented Programming (OOP) Exercise: Classes and Objects Exercises

Create a Bus object that will inherit all of the variables and methods of the parent Vehicle class and
display it.

Expected Output:
Vehicle Name: School Volvo Speed: 180 Mileage: 12

Refer: Inheritance in Python


Show Solution

OOP Exercise 4: Class Inheritance


Given:

Create a Bus class that inherits from the Vehicle class. Give the capacity argument of
Bus.seating_capacity() a default value of 50.

Use the following code for your parent Vehicle class.


class Vehicle:

def __init__(self, name, max_speed, mileage):

self.name = name

self.max_speed = max_speed

self.mileage = mileage

def seating_capacity(self, capacity):

return f"The seating capacity of a {self.name} is {capacity} passengers"


 Run

Expected Output:
The seating capacity of a bus is 50 passengers

Refer:
Show Hint
Show Solution

OOP Exercise 5: Define a property that must have the same value for every
class instance (object)
Define a class attribute”color” with a default value white. I.e., Every Vehicle should be white.

Use the following code for this exercise.


class Vehicle:

def __init__(self, name, max_speed, mileage):

self.name = name

self.max_speed = max_speed

self.mileage = mileage

class Bus(Vehicle):

pass

class Car(Vehicle):

pass
 Run

Expected Output:
Color: White, Vehicle name: School Volvo, Speed: 180, Mileage: 12

Color: White, Vehicle name: Audi Q5, Speed: 240, Mileage: 18

Refer: Class Variable in Python

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-object-oriented-programming-oop-exercise%2F 2/4
8/8/22, 8:26 PM Python Object-Oriented Programming (OOP) Exercise: Classes and Objects Exercises

Show Hint
Show Solution

OOP Exercise 6: Class Inheritance


Given:

Create a Bus child class that inherits from the Vehicle class. The default fare charge of any vehicle
is seating capacity * 100. If Vehicle is Bus instance, we need to add an extra 10% on full fare as a
maintenance charge. So total fare for bus instance will become the final amount = total fare +
10% of the total fare.

Note: The bus seating capacity is 50. so the final fare amount should be 5500. You need to override
the fare() method of a Vehicle class in Bus class.

Use the following code for your parent Vehicle class. We need to access the parent class from
inside a method of a child class.
class Vehicle:

def __init__(self, name, mileage, capacity):

self.name = name

self.mileage = mileage

self.capacity = capacity

def fare(self):

return self.capacity * 100

class Bus(Vehicle):

pass

School_bus = Bus("School Volvo", 12, 50)

print("Total Bus fare is:", School_bus.fare())


 Run

Expected Output:
Total Bus fare is: 5500.0
Show Solution

OOP Exercise 7: Check type of an object


Write a program to determine which class a given Bus object belongs to.

Given:
class Vehicle:

def __init__(self, name, mileage, capacity):

self.name = name

self.mileage = mileage

self.capacity = capacity

class Bus(Vehicle):

pass

School_bus = Bus("School Volvo", 12, 50)


 Run
Show Hint
Show Solution

OOP Exercise 8: Determine if School_bus is also an instance of the Vehicle class

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-object-oriented-programming-oop-exercise%2F 3/4
8/8/22, 8:26 PM Python Object-Oriented Programming (OOP) Exercise: Classes and Objects Exercises

Given:
class Vehicle:

def __init__(self, name, mileage, capacity):

self.name = name

self.mileage = mileage

self.capacity = capacity

class Bus(Vehicle):

pass

School_bus = Bus("School Volvo", 12, 50)


 Run
Show Solution

read://https_pynative.com/?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fpynative.com%2Fpython-object-oriented-programming-oop-exercise%2F 4/4

You might also like