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

Опубликовано: 15 Май 2024
на канале: 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.


На этой странице сайта вы можете посмотреть видео онлайн How to use private variable in python class in Tamil | Encapsulation in python длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь pycodedigi 15 Май 2024, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 217 раз и оно понравилось 7 зрителям. Приятного просмотра!