Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Correcting a Tutorial
#2
(Nov-01-2023, 11:50 PM)sonus89 Wrote: Correct?
It is not correct. The underscore in _protected_member is more of a gentlemen's agreement between programmers. It plays no role in the Python language itself and any function can access these "protected" members. It is every programmer's responsibility to avoid accessing these members in their code.
>>> class A:
...     def _protected(self):
...         print("I'm not protected")
... 
>>> a = A()
>>> a._protected()
I'm not protected
>>> 
The double underscore __private_member only hides the name by mangling it, but it remains accessible, so that encapsulation is not enforced
>>> class A:
...     def __protected(self):
...         print("I'm not protected")
... 
>>> a = A()
>>> a._A__protected()
I'm not protected
Reply


Messages In This Thread
Correcting a Tutorial - by sonus89 - Nov-01-2023, 11:50 PM
RE: Correcting a Tutorial - by Gribouillis - Nov-02-2023, 07:43 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020