为什么cls .__ name__没有出现在dir()中?
发布于 2021-01-29 17:33:02
假设我有一个简单的课程:
class Foobar(object):
pass
如果使用dir(Foobar)
,我将得到以下输出:
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
即使它没有出现在的输出中dir()
,我也可以访问__name__
:
Foobar.__name__
并得到Foobar
。
为什么Python会有这种行为?
关注者
0
被浏览
49