No matter what you study is Python、Java Or other programming languages , I believe everyone has heard of private variables , Because private variables are essential in programming . So you know Python How to define private variables ? Follow Xiaobian and look down .
And Java and C Language use private Keywords define private variables differently ,Python It's a double bar before the variable ( for example ：__test) To mark .
Python Private variables can only be used inside a class , Not called externally , And when the variable is marked private , When calling, you need to insert the class name in front of the variable , Add an underline before the class name , namely “_ClassName__ Variable name ” form .
Python Private variables or methods cannot be called directly in their own classes , You need to define the interface in the class , Implement the reference of private variables 、 Read and modify .
except Python Private variables are used “_” Beyond definition , There are also two special variables , Namely ：
1. _xx What begins with a single underscore is protected Variable of type . That is, the protection type can only be accessed by itself and its subclass . If the internal variable indicates , Such as ： When using “from M import” when , Objects that start with an underscore are not introduced .
2. __xx__ The definition is a special method . Variables or properties within the user controlled namespace , Such as init , __import__ or file . Use only if the document has instructions , Don't define such variables yourself .