클래스에서 __variables
에 액세스하려고하면 파서는 2 개의 밑줄이 현재 클래스에 비공개라고 가정합니다. 비 관련 함수가 "private"변수를 얻는 방법에 유의하십시오.클래스 변수가 아닌 Python 전용 클래스 변수
이것은 버그입니까?
>>> 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']파이썬 2.5 3.2
예상대로 작동합니다. 허락하신다면, 당신이 그들과 함께하는 일은 드물기는하지만, 그 속성은 클래스에 속합니다. – kwatford