Because dir() is supplied primarily as a convenience for use at an
interactive prompt, it tries to supply an interesting set of names
more than it tries to supply a rigorously or consistently defined set
of names, and its detailed behavior may change across releases. For
example, metaclass attributes are not in the result list when the
argument is a class.
因此,我们不应该依赖dir来了解一个对象的所有信息。如果该类实现__dir__方法,我们将只获得从该方法返回的任何内容。他们可能实现了__dir__,并从dir调用中抽象出{}。在
示例:class TestClass:
def __init__(self):
self.myValue = 0
def myMethod(self):
pass
def __dir__(self):
return []
class TestClass1:
def __init__(self):
self.myValue = 0
def myMethod(self):
pass
print dir(TestClass())
print dir(TestClass1())
print dir(TestClass())
print dir(TestClass1())
输出
^{pr2}$