查看dir方法的文檔,里面有這樣的描述:If the object supplies a method named __dir__, it will be used; otherwisethe default dir() logic is used and returns也就是說(shuō),只要對(duì)象本身有__dir__方法,那么實(shí)際上兩者應(yīng)該是一致的。但是嘗試下面這個(gè)例子,發(fā)現(xiàn)兩者并不是一致的:class A(object):
pass
print(dir(A))
print(A.__dir__(A))__dir__方法會(huì)多出一些屬性,例如__mro__, __name__等等,這是為什么呢?
對(duì)于一個(gè)類來(lái)說(shuō),dir()方法和__dir__()方法為什么會(huì)有不同?
慕桂英3389331
2018-10-23 22:31:13