How to use private variable in python class in Tamil | Encapsulation in python

Pubblicato il: 15 maggio 2024
sul canale di: pycodedigi
217
7

How to use private variable in python class in Tamil | Encapsulation in python #python

In Python, the use of a single underscore (_) and double underscores (__) before attribute names serves as a convention rather than a strict language feature. However, they do have different implications in terms of visibility and name mangling:

Single Underscore Prefix (_age):
This is a convention indicating that the attribute is intended to be "protected" or "private" (though not enforced by the language).
It suggests that the attribute should not be accessed directly from outside the class, but there are no technical restrictions preventing such access.

Double Underscore Prefix (__age):
This triggers name mangling in Python.
Name mangling is a technique to make an attribute "private" to its class.
When you define an attribute with double underscores (__), Python internally changes its name to include the class name as a prefix, making it more difficult to access from outside the class.

However, it's still possible to access these "private" attributes by using the mangled name (_ClassName__attributeName), though this is discouraged because it breaks encapsulation.


In questa pagina del sito puoi guardare il video online How to use private variable in python class in Tamil | Encapsulation in python della durata di ore minuti seconda in buona qualità , che l'utente ha caricato pycodedigi 15 maggio 2024, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 217 volte e gli è piaciuto 7 spettatori. Buona visione!