Home » Posts tagged 'Python object-oriented programming'

Tag Archives: Python object-oriented programming

Newly Updated Posts

Data Hiding

In this tutorial, We are going to learn Data Hiding in python.

In Python, data hiding refers to the concept of making certain attributes or methods of a class inaccessible from outside the class. This is done to encapsulate the internal implementation details of a class and to prevent direct modification or access to sensitive data.

There are two main ways to achieve data hiding in Python: name mangling and convention-based hiding.

1.Name Mangling: Name mangling is a technique that prefixes the name of an attribute with double underscores (__). When Python encounters a double underscore prefix, it automatically modifies the attribute name to include the class name as a prefix. This makes the attribute effectively hidden from outside the class, although it can still be accessed using the mangled name.

Here’s an example:

class MyClass:
    def __init__(self):
        self.__hidden_attribute = 42

    def __hidden_method(self):
        print("This is a hidden method.")

obj = MyClass()
print(obj._MyClass__hidden_attribute)  # Accessing the hidden attribute
obj._MyClass__hidden_method()  # Calling the hidden method

Output:
42
This is a hidden method.

Note that using name mangling does not completely prevent access to the hidden attributes or methods, but it serves as a convention to discourage direct access.

2. Convention-Based Hiding: Python does not enforce strict visibility rules like some other languages. Instead, it relies on conventions to indicate the intended visibility of attributes and methods. By convention, a single underscore (_) prefix is used to indicate that an attribute or method should be considered private or hidden.

Here’s an example:

class MyClass:
    def __init__(self):
        self._hidden_attribute = 42

    def _hidden_method(self):
        print("This is a hidden method.")

obj = MyClass()
print(obj._hidden_attribute)  # Accessing the hidden attribute
obj._hidden_method()  # Calling the hidden method

Output:
42
This is a hidden method.

Convention-based hiding is more lenient and relies on the developers’ understanding and agreement to respect the convention.

It’s important to note that data hiding in Python is not meant to provide strict access control, but rather to indicate the intended visibility of attributes and methods. Python encourages developers to follow the principle of “we are all consenting adults here,” trusting that they will respect the intended conventions.

Class,Object and Members

In this tutorial we will learn Class, Object and Members in Python.

In Python, classes are used to create objects, which are instances of those classes. Classes serve as blueprints or templates for creating objects with similar characteristics and behaviors. Each object created from a class is called an instance or an object of that class.

A class defines the properties (also known as attributes) and behaviors (also known as methods) that objects of that class will have. Attributes are variables that store data, while methods are functions that define the behavior of the class.

Here’s an example of a simple class definition in Python:

class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year

    def start_engine(self):
        print("The engine has started.")

    def drive(self):
        print("The car is being driven.")

    def stop_engine(self):
        print("The engine has stopped.")

In the above example, Car is the class, and make, model, and year are its attributes. The __init__ method is a special method called the constructor, which is executed when an instance of the class is created. It initializes the attributes of the object using the values passed as arguments.

The start_engine, drive, and stop_engine methods are behaviors associated with the Car class. They define the actions that objects of the Car class can perform.

To create an instance of the Car class, you can do the following:

my_car = Car("Toyota", "Camry", 2022)

Here, my_car is an object of the Car class. It has the attributes make (set to “Toyota”), model (set to “Camry”), and year (set to 2022). You can access the attributes and call the methods of the object using dot notation, like this:

print(my_car.make) # Output: Toyota

print(my_car.model) # Output: Camry

print(my_car.year) # Output: 2022

my_car.start_engine() # Output: The engine has started.

my_car.drive() # Output: The car is being driven.

my_car.stop_engine() # Output: The engine has stopped.

In summary, classes define the structure and behavior of objects, while objects are instances of those classes. Members refer to the attributes and methods associated with a class or an object. Attributes store data, and methods define the behaviors or actions that can be performed by the class or object.