Python OOP Fundamentals: Classes, Objects, Inheritance, and Constructors Explained
Python Object‑Oriented Programming (OOP)
Python’s OOP model lets developers model real‑world entities as objects defined by classes. The language prioritises code reuse and modularity, making it straightforward to create and extend classes.
What Is a Class?
A class is a logical container that bundles data (attributes) and behaviour (methods). It defines a template for objects that share common properties.
Example: A Customer class could expose attributes such as transactions, balance, and debt, along with methods to deposit() or withdraw().
Defining a Class in Python
Follow these steps to create a simple class:
- Use the
classkeyword:class MyClass: pass - Define methods inside the class. The first parameter of every instance method is
self, which refers to the object itself.class MyClass: def greet(self): print("Hello from MyClass") - Instantiate the class:
obj = MyClass()
- Call its methods:
obj.greet()
Complete example:
# Working example of a Python class
class MyClass:
def method1(self):
print("Guru99")
def method2(self, text):
print("Software Testing: " + text)
def main():
c = MyClass()
c.method1()
c.method2(" Testing is fun")
if __name__ == "__main__":
main()
Inheritance in Python
Inheritance allows a derived class to reuse and extend the behaviour of a base class. Python supports both single and multiple inheritance.
class Base:
def greet(self):
print("Hello from Base")
class Derived(Base):
def greet(self):
super().greet()
print("Hello from Derived")
# Usage
obj = Derived()
obj.greet()
Key points:
- Derived classes inherit attributes and methods automatically.
- Override methods by defining a method with the same name in the subclass.
- Call a parent method explicitly with
ParentClass.method(self, …)orsuper().method().
Constructors: __init__
The __init__ method initializes new objects with predefined values.
class User:
def __init__(self, name):
self.name = name
def say_hello(self):
print(f"Welcome to Guru99, {self.name}")
user = User("Alex")
user.say_hello()
Output:
Welcome to Guru99, Alex
Python 2 vs Python 3
Only minor syntax differences exist. The example above works in both versions with appropriate print statements.
Key Takeaways
- Classes bundle data and behaviour, defined with the
classkeyword. - Instance methods use
selfto refer to the object. - Inheritance promotes code reuse and can be single or multiple.
- Constructors (
__init__) set initial state for new objects. - Python’s flexible syntax and runtime handling of
selfmake OOP intuitive.
Python
- Mastering Python Inheritance: Concepts, Syntax, and Practical Examples
- Master Python Multiple Inheritance, Multilevel Inheritance, and Method Resolution Order (MRO)
- C++ Classes & Objects: A Practical Guide with Code Examples
- Master C# Inheritance & Polymorphism: Practical Code Examples
- C# Serialization & Deserialization: A Practical Example
- Understanding Java Classes and Objects: Clear Concepts, Practical Examples
- Java Inheritance Explained: Types, Syntax, and Practical Examples
- Python Counter in collections – Efficient Counting, Updating, and Arithmetic Operations
- Understanding type() and isinstance() in Python: Practical Examples
- Master Python's Object-Oriented Programming: A Comprehensive Guide