Python Forum
Correcting a Tutorial - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: Correcting a Tutorial (/thread-41043.html)



Correcting a Tutorial - sonus89 - Nov-01-2023

Hi everyone!

Tutorialspoint has a Python Tutorial but I think I found an error there.

This section:
https://www.tutorialspoint.com/python/python_vs_cpp.htm

Says this:
"C++ supports the concept of data encapsulation as the visibility of the variables can be defined as public, private and protected.
Python doesn't have the provision of defining the visibility. ..."

Python can define visibility for methods:
def public_method()
def _protected_method()
def __private_method()
As well as visibility for attributes:
self.public_attribute
self._protected_attribute
self.__private_attribute
Correct?


RE: Correcting a Tutorial - Gribouillis - Nov-02-2023

(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