Object-oriented languages, such as C++ and Java, control the access to class resources by public, private, and protected keywords. Python can also introduce a concept of public, protected, and private. But all the variables in Python are public.
Private variables of the class are denied access from the environment outside the class, while public variables are accessible from outside the class. Protected variables of a class are accessible from within the class and are also available to its sub-classes. No other environment is permitted access to it. This enables specific resources of the parent class to be inherited by the child class.
Public variables (generally methods declared in a class) are accessible from outside the class. All the variables in a Python class are public by default. Any variables can be accessed from outside the class environment.
Protected variables
Python's convention to make an instance variable protected is to add a prefix _ (single underscore) to it. This effectively prevents it from being accessed unless it is from within a sub-class.
Private variables
The double underscore __ prefixed to a variable makes it private. It gives a strong suggestion not to touch it from outside the class. Any attempt to do so will result in an AttributeError.
The code below defines a class that set the instance attributes in public, protected, and private, in the __init__() function.
Access to the public attribute.
The same happens with proteced variables in Python.
Adding prefix of double underscore works, and if accessed AttributeError arises.
But private can be changed to public in Python. Variables with a double underscore will be textually replaced with object.__classname__ variable, then it can still be accessed from outside the class.
Citation:
https://docs.python.org/3/tutorial/classes.html#private-variables
https://www.tutorialsteacher.com/python/public-private-protected-modifiers
Private variables of the class are denied access from the environment outside the class, while public variables are accessible from outside the class. Protected variables of a class are accessible from within the class and are also available to its sub-classes. No other environment is permitted access to it. This enables specific resources of the parent class to be inherited by the child class.
Declaration methods of valiables
Public variablesPublic variables (generally methods declared in a class) are accessible from outside the class. All the variables in a Python class are public by default. Any variables can be accessed from outside the class environment.
Protected variables
Python's convention to make an instance variable protected is to add a prefix _ (single underscore) to it. This effectively prevents it from being accessed unless it is from within a sub-class.
Private variables
The double underscore __ prefixed to a variable makes it private. It gives a strong suggestion not to touch it from outside the class. Any attempt to do so will result in an AttributeError.
The code below defines a class that set the instance attributes in public, protected, and private, in the __init__() function.
class PersonalName: def __init__( self, public: str = 'Dr. Ramoray', protect: str = 'Ken Adams', private: str = 'Joey Tribbiani', ): self.public = public self._protect = protect self.__private = privateCreate the instance of the class.
joey = PersonalName('Dr. Ramoray', 'Ken Adams', 'Joey Tribbiani')
Access to the public attribute.
print(joey.public) # Dr. RamorayModify this public attribute.
joey.public = 'Chandler Muriel Bing' print(joey.public) # Chandler Muriel BingPublic variables can be accessed and modified from outside of the class.
The same happens with proteced variables in Python.
print(joey._protect) joey._protect = 'Regina Phalange' print(joey._protect) # Ken Adams # Regina PhalangeIn fact, The underscore _ prefixed variable doesn't prevent instance variables from accessing or modifying the instance.
Adding prefix of double underscore works, and if accessed AttributeError arises.
joey.__private # AttributeError: 'PersonalName' object has no attribute '__private'
But private can be changed to public in Python. Variables with a double underscore will be textually replaced with object.__classname__ variable, then it can still be accessed from outside the class.
print(joey._PersonalName__private) # Joey TribbianiPrivate variables that cannot be accessed from outside don’t exist in Python.
Citation:
https://docs.python.org/3/tutorial/classes.html#private-variables
https://www.tutorialsteacher.com/python/public-private-protected-modifiers
Comments
Post a Comment