python object-oriented three access rights underline double underline

Keywords: Python Attribute

1, Double underline

If you want internal properties not to be accessed externally, you can add two underscores to the name of the property. In Python, if the variable name of an instance starts with \\\\\\\\\\.

Are instance variables starting with double underscores necessarily inaccessible from outside? Not really. The reason why you can't directly access the name variable is that the Python interpreter has changed the name variable to student name, so you can still access the name variable through student name.

The external code can set the variable name, but in fact, the variable name and the variable name inside the class are not a variable! The internal name variable has been automatically changed to student name by the Python interpreter, while the external code adds a new name variable to the instance.

 1 >>> class Student(object):
 2 ...     def __init__(self, name):
 3 ...         self.__name = name
 4 ...     def get_name(self):
 5 ...         return self.__name
 6 ...     def set_name(self, name):
 7 ...         self.__name = name
 8 ... 
 9 >>> st = Student('Jack')
10 >>> st.get_name()
11 'Jack'
12 >>> st.set_name('Mike')
13 >>> st.get_name()
14 'Mike'
15 >>> dir(st)
16 ['_Student__name', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'get_name', 'set_name']
17 >>> st.__name
18 Traceback (most recent call last):
19   File "<stdin>", line 1, in <module>
20 AttributeError: 'Student' object has no attribute '__name'
21 >>> st._Student__name
22 'Mike'
23 >>> st.__name = 'New'   # Added a new variable to the instance
24 >>> st.__name 
25 'New'
26 >>> st._Student__name
27 'Mike'

2, Single underline

Sometimes, you will see instance variable names that start with an underscore, such as ﹣ name '. Such instance variables can be accessed externally. However, according to the Convention, when you see such variables, it means, "although I can be accessed, but it is, please treat me as a private variable, and do not access at will.".

1 >>> class Student(object):
2 ...     def __init__(self, name):
3 ...         self._name = name
4 ... 
5 >>> st = Student('Jack')
6 >>> st._name
7 'Jack'

3, Special variable

Variables starting with double underscores and ending with double underscores. Special variables can be accessed directly, not private variables.

 1 >>> class Student(object):
 2 ...     def __init__(self, name):
 3 ...         self.__name = name
 4 ...     def get_name(self):
 5 ...         return self.__name
 6 ... 
 7 >>> st = Student('Jack')
 8 >>> st.get_name()
 9 'Jack'
10 >>> st.__init__('Mike')
11 >>> st.get_name()
12 'Mike'

Posted by payjo on Tue, 12 May 2020 08:29:46 -0700