Object-Oriented Programming (OOP) is a programming paradigm that focuses on creating objects that contain both data and behavior. In Python, everything is an object, and the language fully supports OOP. Here's an overview of how to use OOP in Python:
Classes and Objects
In OOP, a class is a blueprint for creating objects, while an object is an instance of a class. A class defines attributes (data) and methods (behavior) that are common to all objects of that class.
To define a class in Python, you use the class keyword followed by the name of the class. Here's an example:
pythonclass Person:
def __init__(self, name, age):
self.name = name
self.age = age
def say_hello(self):
print(f"Hello, my name is {self.name} and I'm {self.age} years old.")
In this example, we define a Person class with two attributes (name and age) and one method (say_hello). The __init__ method is a special method that is called when an object of the class is created, and it initializes the object's attributes.
To create an object of a class, you call the class like a function. Here's an example:
pythonperson = Person("Alice", 30)
This creates a Person object with the name "Alice" and age 30.
Inheritance
Inheritance is a way to create a new class based on an existing class. The new class, called a subclass, inherits all the attributes and methods of the existing class, called the superclass. The subclass can also add its own attributes and methods.
To create a subclass in Python, you define a new class and specify the superclass in parentheses after the class name. Here's an example:
pythonclass Student(Person):
def __init__(self, name, age, student_id):
super().__init__(name, age)
self.student_id = student_id
def say_hello(self):
print(f"Hello, my name is {self.name}, I'm {self.age} years old, and my student ID is {self.student_id}.")
In this example, we define a Student class that inherits from the Person class. The __init__ method calls the superclass's __init__ method using the super function, and adds a new attribute (student_id). The say_hello method overrides the superclass's method to include the student ID.
Encapsulation
Encapsulation is a way to restrict access to an object's attributes and methods from outside the object. In Python, you can use naming conventions to indicate which attributes and methods are meant to be private (not accessed from outside the object).
In Python, an attribute or method that starts with two underscores (__) is considered private, and can only be accessed from within the object. Here's an example:
pythonclass MyClass:
def __init__(self):
self.__private_attribute = 42
def __private_method(self):
print("This is a private method.")
def public_method(self):
print(f"The private attribute is {self.__private_attribute}.")
self.__private_method()
In this example, we define a MyClass class with a private attribute (__private_attribute) and a private method (__private_method). The public_method method is a public method that can be accessed from outside the object, and it uses the private attribute and method. Note that to access a private attribute or method from within the object, you still need to use the double underscore prefix
0 Comments