Best Way to Use Python Encapsulation Method

Encapsulation is a fundamental principle in object-oriented programming (OOP) that focuses on bundling data and methods into a single unit called a class. It provides the means for data hiding and abstraction, allowing you to control the accessibility and visibility of class members from the outside.

In Python Classes in Pune, encapsulation can be achieved through the use of access modifiers and property decorators. There are three commonly used access modifiers in Python:

  1. Public: By default, all class members (attributes and methods) are considered public and can be accessed from anywhere. You can access public members directly using the dot notation (object.member).

  2. Protected: Protected members are denoted by a single underscore prefix (_). While they can still be accessed from outside the class, it is considered a convention to treat them as non-public. It indicates that these members should not be accessed or modified directly from outside the class, although Python does not enforce this.

  3. Private: Private members are denoted by a double underscore prefix (__). Python performs name mangling, which means that the actual name of a private member is changed to include the class name as a prefix to avoid accidental name clashes between different classes. Private members are not accessible directly from outside the class. However, they can still be accessed using the mangled name (_ClassName__member), although it is generally discouraged.

Here's an example to illustrate encapsulation in Python:

class MyClass:

    def __init__(self):

        self.public_member = "Public"       # Public member

        self._protected_member = "Protected" # Protected member

        self.__private_member = "Private"   # Private member


    def public_method(self):

        print("This is a public method.")


    def _protected_method(self):

        print("This is a protected method.")


    def __private_method(self):

        print("This is a private method.")


obj = MyClass()


print(obj.public_member)           # Output: Public

print(obj._protected_member)       # Output: Protected

print(obj._MyClass__private_member) # Output: Private (using name mangling)


obj.public_method()           # Output: This is a public method.

obj._protected_method()       # Output: This is a protected method.

obj._MyClass__private_method() # Output: This is a private method.


Remember that the use of access modifiers is more of a convention in Python, and it's up to the developer to respect encapsulation rules. Python Training in Pune favors a philosophy of "we are all consenting adults here," trusting programmers to use the appropriate level of encapsulation and not accessing or modifying members that should be treated as non-public.


Address: 1st Floor Office No 23, Dnyaneshwar Paduka chowk, A-wing, Fergusson College Rd, Sud Nagar, Shivajinagar, Pune, Maharashtra 411005

Ansichten 262
Aktie
Kommentar
Emoji
😀 😁 😂 😄 😆 😉 😊 😋 😎 😍 😘 🙂 😐 😏 😣 😯 😪 😫 😌 😜 😒 😔 😖 😤 😭 😱 😳 😵 😠 🤔 🤐 😴 😔 🤑 🤗 👻 💩 🙈 🙉 🙊 💪 👈 👉 👆 👇 🖐 👌 👏 🙏 🤝 👂 👃 👀 👅 👄 💋 💘 💖 💗 💔 💤 💢
Sie können auch mögen