In this Advance Python Programming article we are going to talk about Classes and Objects in Python, first of all let’s talk about Python, so Python is powerful and popular programming language used for different types of applications, you can use Python in scientific computing to web development. one of the best features of Python programming language is support of Object Oriented Programming or we can say OOP, and this features allows programmers to write clean, modular and well organized codes.
In this article we want to dive into advance Python programming concepts related to classes and objects, which are the building blocks of OOP in Python.
Classes and Objects
In the heart of OOP there is concept of classes, so Python classes are blueprint or template for creating objects. object is simply an instance of a class, which contains data and functions that operate on that data.
For defining a class in Python, you use class keyword followed by the name of the class:
1 2 |
class MyClass: # class definition goes here |
Inside the class definition, you can define attributes and methods using def keyword:
1 2 3 4 5 6 7 |
class MyClass: # attribute definition attr = "geekscoders" # method definition def my_hello(self): print(self.attr) |
In the above example we have defined a class MyClass with a single attribute attr and a single method my_hello(). self parameter in the method definition refers to the instance of the class that the method is being called on.
For creating an object of a class, you use class name followed by parentheses:
1 |
obj = MyClass() |
This creates a new instance of MyClass class and assigns it to the variable obj. now we can call my_hello() method on the object:
1 |
obj.my_hello() |
This is the complete code
1 2 3 4 5 6 7 8 9 10 11 |
class MyClass: # attribute definition attr = "geekscoders" # method definition def my_hello(self): print(self.attr) obj = MyClass() obj.my_hello() |
This will be the result
Inheritance
One of the key benefits of OOP is the ability to create new classes that are based on existing classes. this is known as inheritance, and it allows you to reuse code and create more specialized classes.
For creating a subclass, you can define a new class that inherits from an existing class, In this example we have defined a subclass MySubclass that inherits from MyClass class.
1 2 |
class MySubclass(MyClass): # subclass definition goes here |
Now we can create objects of the subclass, which will inherit all of the attributes and methods of the base classe.
1 2 |
obj2 = MySubclass() obj2.my_hello() |
This is the complete code, and you can see in here we can access attribute and methods of parent class inside our subclass.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class MyClass: # attribute definition attr = "geekscoders.com" # method definition def my_hello(self): print(self.attr) class MySubclass(MyClass): pass obj2 = MySubclass() obj2.my_hello() |
This will be the result
Polymorphism
Another key concept in OOP is polymorphism, polymorphism allows objects of different classes to be used interchangeably in the same context. this is achieved through method overriding, where a subclass provides a different implementation of a method inherited from the superclass.
For example, let’s say we want to create a new subclass of MyClass called MyOtherSubclass, but we want my_hello() method to output a different message:
1 2 3 |
class MyOtherSubclass(MyClass): def say_hello(self): print("goodbye") |
Now we can create objects of both subclasses and call their my_hello() methods:
1 2 3 4 5 |
obj3 = MyOtherSubclass() obj3.my_hello() obj4 = MySubclass() obj4.my_hello() |
This is the complete code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class MyClass: # attribute definition attr = "geekscoders.com" # method definition def my_hello(self): print(self.attr) class MySubclass(MyClass): pass class MyOtherSubclass(MyClass): def my_hello(self): print("goodbye") obj3 = MyOtherSubclass() obj3.my_hello() obj4 = MySubclass() obj4.my_hello() |
And this will be the result
Learn More
- Introduction to Python Classes and Objects
- Python Static Vs Instance Methods
- Python Abstraction and Encapsulation
- Python Class Inheritance and Polymorphism
- Key difference Between Python Classes and Objects
- How to Debug Python Classes and Objects
- Python Classes and Objects from Scratch
- Best Tips for Python Classes and Objects