爲什麼在屬性字典中沒有「locked:True」?
因爲你正在檢查例如快譯通,但locked
是一個類屬性。你可以使用self.__class__.__dict__
找到它。
如果您需要看到的所有屬性,包括繼承和class屬性,使用insect.getmembers()
或內置dir
功能:
>>> import inspect
>>> class A(object):
... b = 1
... def __init__(self):
... self.c = 1
...
>>> a = A()
>>> inspect.getmembers(a)
[('__class__', <class '__main__.A'>), ('__delattr__', <method-wrapper '__delattr__' of A object at 0x10d47f1d0>), ('__dict__', {'c': 1}), ('__doc__', None), ('__format__', <built-in method __format__ of A object at 0x10d47f1d0>), ('__getattribute__', <method-wrapper '__getattribute__' of A object at 0x10d47f1d0>), ('__hash__', <method-wrapper '__hash__' of A object at 0x10d47f1d0>), ('__init__', <bound method A.__init__ of <__main__.A object at 0x10d47f1d0>>), ('__module__', '__main__'), ('__new__', <built-in method __new__ of type object at 0x10d0d7280>), ('__reduce__', <built-in method __reduce__ of A object at 0x10d47f1d0>), ('__reduce_ex__', <built-in method __reduce_ex__ of A object at 0x10d47f1d0>), ('__repr__', <method-wrapper '__repr__' of A object at 0x10d47f1d0>), ('__setattr__', <method-wrapper '__setattr__' of A object at 0x10d47f1d0>), ('__sizeof__', <built-in method __sizeof__ of A object at 0x10d47f1d0>), ('__str__', <method-wrapper '__str__' of A object at 0x10d47f1d0>), ('__subclasshook__', <built-in method __subclasshook__ of type object at 0x7fc6b8638070>), ('__weakref__', None), ('b', 1), ('c', 1)]
>>> dir(a)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'b', 'c']
>>> a.__class__.__dict__
dict_proxy({'__module__': '__main__', 'b': 1, '__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None, '__init__': <function __init__ at 0x10d4791b8>})
謝謝!我不知道如何使用'self .__ class __.__ dict__'雖然,這不是在命令行嗎? – user61629
@ user61629我已更新示例 – warvariuc
再次感謝您。 – user61629