There are no access-controlled keywords in python, such as private, protected (Java), and so on. However, in the Python code, there are conventions for access control.
1, single underline "_" in Python, through the single underline "" to achieve the module level of privatization, except for variables. General Conventions A function that begins with a single underline "" is private to the module, meaning that "from modulename import *" will not introduce a function that begins with a single underscore "_".
Now there is a module example_example.py, the content is as follows, the module in a variable name and a function name, respectively, with "_" Start:
Name = ' Bruce '
_tall = 180
def call_for ():
print (' name is: ', name]
print (' _tall is ', _tall)
def _ Call_for ():
print (' name is: ", name)
#_call_for = _call_for ()
print (' _tall is ', _tall)
_tall is 180
Call the script again:
#调用脚本模块example_example
Import example_example
#调用不带下划线变量
example_example.name
out[12]: ' Bruce '
#调用带下划线变量
example_example._tall #对于变量单下划线不会影响调用
out[13]: 180
#调用不带下划线函数
Example_ Example.call_for ()
out[16]:
name Is:bruce
_tall is 180
#调用不带下划线函数会报错
example_example._ Call_for ()
Traceback (most recent call last):
File ' <ipython-input-15-e642b1386946> ', line 1, in < Module>
example_example._call_for ()
typeerror: ' Nonetype ' object is not callable
2, double underline "__" for class properties in Python, you can use the double underline "__" to achieve a certain degree of privatization , because the property at the beginning of the double underscore will be "confused" (mangling).
class Person (object):
tall = 180
Hobbies = []
def __init__ (self, Name, age,weight):
self.name = name
Self.age = age
self.weight = weight
self.__id = 430
@classmethod
def infoma (CLS):
print (CLS). __id)
# Person.infoma ()
bruce = person ("Bruce", 25,180) print (
bruce.age)
print (bruce.__id)
25
---------------------------------------------------------------------------
attributeerror traceback (most recent call last)
<ipython-input-32-ae0c1d7abe5a> in <module> ()
bruce = Person ("Bruce", 25,180)
print ( bruce.age)
---> Print (bruce.__id)
attributeerror: ' Person ' object has no attribute ' __id '
In fact, through the built-in function dir () you can see some of the original, "__address" property at run time, the property name was changed to "_person__address" (the property name before the addition of a single underline and class name)
Print (dir (Bruce))
#可以看到Bruce中有_person__Id的属性, compared to the original __id attribute, becomes callable [' __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__ ', ' _person__id ', ' age ', ' hobbies ', ' infoma ', ' name ', ' tall ', ' weight '
So, even if it is a double underline, there is no privatization of the property, because the "__address" attribute can be accessed directly in the following way:
Print (bruce._person__id)
430 #通过使属性__Id名前增加了单下划线_和类名person来实现属性的可调用
Another important purpose of the double underline is to avoid the conflict of subclasses on the property with the same name as the parent class
Class A (object):
def __init__ (self):
self.__private ()
self.public ()
def __private (self):
Print (' a.__private () ')
def public (self):
print (' A.public () ')
class B (A):
def __private (self):
print (' b.__private () ')
def public (self):
print (' B.public () ')
B = B ()
A.__private ()
B.public ()
When instantiating B, because no _ _init_ function is defined, the parent class's _ _init_ _ is invoked, but because of the "obfuscation" effect of the double underline, "self.__private ()" becomes "self._a__private ()".
Summarize:
The use of "_" and "__" is more of a specification/agreement, and does not really achieve the purpose of the restriction: "_": a single underscore is a variable of type protected , that is, it can only be accessed by itself and by subclasses , while indicating weak internal variable markings, such as when using "from Modulenmae import *" , an object beginning with an underscore will not be introduced. "__": a double underline represents a variable of a private type . You can only allow access to this class itself, even subclasses, which, at runtime, have a single underline and class name for the property name .