0% found this document useful (0 votes)
10 views

Classes in Python

The document discusses classes in Python, including advantages of classes, creating classes, instance variables, object creation, the __init__ method, class variables, private variables and methods, class methods, inner classes, inheritance, the super() method, multiple inheritance, multilevel inheritance, method resolution order, operator overloading, and public, private, and protected members.

Uploaded by

siddharthsh22150
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Classes in Python

The document discusses classes in Python, including advantages of classes, creating classes, instance variables, object creation, the __init__ method, class variables, private variables and methods, class methods, inner classes, inheritance, the super() method, multiple inheritance, multilevel inheritance, method resolution order, operator overloading, and public, private, and protected members.

Uploaded by

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

Classes in python

Advantages of classes
● Classes provide an easy way of keeping the data members and methods
together in one place which helps in keeping the program more organized.
● Using classes also provides inheritance.
● Classes also help in overriding any standard operator.
● Using classes provides the ability to reuse the code which makes the
program more efficient.
● Grouping related functions and keeping them in one place (inside a class)
provides a clean structure to the code which increases the readability of
the program.
Creating class

Instance variable

Object creation

Object calling
_init_ method : Constructors
● The __init__ method act as
constructors in python.
● Constructors are used to initializing
the object’s state.
● Like methods, a constructor also
contains a collection of
statements(i.e. instructions) that are
executed at the time of Object
creation. It runs as soon as an object
of a class is instantiated. The method
is useful to do any initialization you
want to do with your object.
● You can assign values to instance
variables during the object's creation
by passing arguments to the
constructor.
Class variables
Class variables
are the variables
whose single copy
is available to all
instances of the
class
Private
variable and
methods

Private variable

Use double
underscore with
variable
Class methods
Inner Class
Inheritance
Inheritance is the capability of one class to derive or inherit the properties
from another class.
The benefits of inheritance are:
● It represents real-world relationships well.
● It provides reusability of a code.
● it allows us to add more features to a class without modifying it.
● It is transitive in nature, which means that if class B inherits from another
class A, then all the subclasses of B would automatically inherit from class A
Inheritance syntax
constructor in inheritance

● constructor of super class is available to sub class


● Constructor is always overridden in subclass (if exists)
● Method with same name in subclass as in super class are also overridden in
subclass
super

super() is used to call


the constructor and
methods of super class
Python Multiple Inheritance
● A class can be derived from more
than one base class in Python,
This is called multiple inheritance.
● In multiple inheritance, the
features of all the base classes are
inherited into the derived class.
The syntax for multiple inheritance
is similar to single inheritance.
Multiple Inheritance
Python Multilevel Inheritance
We can also inherit from a
derived class. This is
called multilevel
inheritance. It can be of
any depth in Python.

In multilevel inheritance,
features of the base class
and the derived class are
inherited into the new
derived class.
Method Resolution Order in Python
Every class in Python is derived from the object class. It is the most base type
in Python. So technically, all other classes, either built-in or user-defined, are
derived classes and all objects are instances of the object class.

In the multiple inheritance scenario, any specified attribute is searched first in


the current class. If not found, the search continues into parent classes in
depth-first, left-right fashion without searching the same class twice.
Operator overloading
Allows the same operator to have
different meaning according to the
context is called operator
overloading.
● Python operators work for built-in
classes.
● Same operator behaves differently
with different types. For example, the
+ operator will perform arithmetic
addition on two numbers, merge two
lists, or concatenate two strings.
Public , Private , Protected members
Default :- All members in a Python class are public by default.
Public Member:- As per Python’s convention any variable which doesn’t contains
double underscore as prefix is public member variable. Any member can be
accessed from outside the class environment.
Private variable:- Private variables means you can use them only inside the
class. It’ll give you an error as soon as you use it or access it outside the class.
But you can always use private member variable inside the class or any method of
that class. Use __ for private
Private method:- Use double underscores as prefix in any method to make that
private.
End

You might also like