Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Python

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:

  1. Use the class keyword:
    class MyClass:
        pass
  2. 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")
  3. Instantiate the class:
    obj = MyClass()
  4. 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:

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

Python

  1. Mastering Python Inheritance: Concepts, Syntax, and Practical Examples
  2. Master Python Multiple Inheritance, Multilevel Inheritance, and Method Resolution Order (MRO)
  3. C++ Classes & Objects: A Practical Guide with Code Examples
  4. Master C# Inheritance & Polymorphism: Practical Code Examples
  5. C# Serialization & Deserialization: A Practical Example
  6. Understanding Java Classes and Objects: Clear Concepts, Practical Examples
  7. Java Inheritance Explained: Types, Syntax, and Practical Examples
  8. Python Counter in collections – Efficient Counting, Updating, and Arithmetic Operations
  9. Understanding type() and isinstance() in Python: Practical Examples
  10. Master Python's Object-Oriented Programming: A Comprehensive Guide