Python OOP

Object-Oriented Programming in Python

Python - OOP Concepts:

Object-oriented programming (OOP) is a programming paradigm that revolves around the concept of objects, which can contain data in the form of fields (attributes or properties), and code in the form of procedures (methods or functions). Python is a versatile language that fully supports OOP. Here are some fundamental OOP concepts in Python:


1. Class: A class is a blueprint for creating objects. It defines the attributes and methods that will be associated with objects created from that class.

class MyClass:
⠀⠀pass

2. Object/Instance: An object is an instance of a class. It is a concrete entity based on a class, and it has its own unique attributes and methods.

obj = MyClass()  # Creating an object of MyClass

3. Attributes: Attributes are data stored within a class or instance and represent the state or characteristics of the object.

PDF Copy Code
class MyClass:
    def __init__(self, x):
        self.x = x

obj = MyClass(5)
print(obj.x)
Output:
5

4. Methods: Methods are functions defined within a class. They operate on the object's data and can also modify the object's state.

PDF Copy Code
class MyClass:
    def __init__(self, x):
        self.x = x
    
    def display(self):
        print(self.x)

obj = MyClass(5)
obj.display() 
Output:
5

5. Constructor: The '__init__' method is a special method in Python classes used to initialize newly created objects. It is called when the object is created.

PDF Copy Code
class MyClass:
    def __init__(self, x):
        self.x = x

obj = MyClass(5)

6. Inheritance: Inheritance is the mechanism of creating a new class by deriving from an existing class. The new class inherits attributes and methods from the parent class.

PDF Copy Code
class ParentClass:
    def parent_method(self):
        print("Parent method")

class ChildClass(ParentClass):
    def child_method(self):
        print("Child method")

obj = ChildClass()
obj.parent_method()
Output:
Parent method

7. Encapsulation: Encapsulation is the bundling of data (attributes) and methods that operate on the data into a single unit (class). It hides the internal state of an object from the outside world.

PDF Copy Code
class MyClass:
    def __init__(self, x):
        self.__x = x  # Private attribute
    
    def display(self):
        print(self.__x)

obj = MyClass(5)
obj.display()  # Output: 5
# obj.__x  # This would result in an AttributeError
Output:
5

8. Polymorphism: Polymorphism allows methods to do different things based on the object that it is called on. It allows for flexibility and modularity in code.

PDF Copy Code
class Dog:
    def sound(self):
        print("Woof")

class Cat:
    def sound(self):
        print("Meow")

def make_sound(animal):
    animal.sound()

dog = Dog()
cat = Cat()

make_sound(dog)  # Output: Woof
make_sound(cat)  # Output: Meow
Output:
Woof
Meow

Remember: These are some of the fundamental concepts of object-oriented programming in Python. Understanding and applying these concepts can greatly improve the organization, flexibility, and reusability of your code.

What's Next?

We've now entered the finance section on this platform, where you can enhance your financial literacy.