+ -
当前位置:首页 → 问答吧 → python中为何无法查询类的__name__属性(有示例)?

python中为何无法查询类的__name__属性(有示例)?

时间:2010-11-04

来源:互联网

Python code

class A(object):
    "This ia A Class"
    pass

print(A.__dict__.keys(),"\n")
print(dir(A),"\n")
print(A.__name__)



类A中确实有__name__属性
为什么用A.__dict__.keys()和dir(A)查询不到有__name__属性?
如何才能查到这个属性

作者: Dic4000   发布时间: 2010-11-04

是私有的吧。所以不能访问

作者: codesnail   发布时间: 2010-11-04

Python code

>>> help(A)
Help on class A in module __main__:

class A(__builtin__.object)
 |  This ia A Class
 |  
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)

>>> 
>>> dir(__builtins__.object)
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__']
>>> dir(__builtins__.object.__class__)
['__base__', '__bases__', '__basicsize__', '__call__', '__class__', '__cmp__', '__delattr__', '__dict__', '__dictoffset__', '__doc__', '__flags__', '__getattribute__', '__hash__', '__init__', '__itemsize__', '__module__', '__mro__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__subclasses__', '__weakrefoffset__', 'mro']
>>> 

作者: selecthis   发布时间: 2010-11-04

好像是个bug

作者: I_NBFA   发布时间: 2010-11-04

热门下载

更多