Python private class variables that aren't class variables

When trying to access __variables from a class, the parser assumes the 2 underscores are private relative to the current class. Notice how an unrelated function gets a "private" variable.

Is this a bug?

>>> def f(): pass
...
>>> class A:
...     def g(self):
...             f.__x = 1
...             def h():
...                     pass
...             h.__y = 2
...             return h
...
>>> z = A().g()
>>> dir(z)
['_A__y', '__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get_
_', '__getattribute__', '__hash__', '__init__', '__module__', '__name__', '__new
__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'func_
closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals',
 'func_name']
>>> dir(f)
['_A__x', '__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get_
_', '__getattribute__', '__hash__', '__init__', '__module__', '__name__', '__new
__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'func_
closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals',
 'func_name']

Tested on python 2.5 and 3.2


This is a well documented behavior.

This mangling is done without regard to the syntactic position of the identifier, as long as it occurs within the definition of a class.

链接地址: http://www.djcxy.com/p/78566.html

上一篇: 无法将对象变量作为默认值传递给对象方法

下一篇: 不是类变量的Python私有类变量