dir()和__dir__之间有什么区别?

时间:2013-10-31 09:59:32

标签: python

在python中python中dir()函数和__dir__属性之间的区别是什么?

>>> 2 .__dir__()
['__divmod__', 'real', '__rxor__', '__floor__', '__hash__', '__index__', '__lt__', '__ceil__', '__repr__', '__reduce_ex__', '__rpow__', '__rand__', '__truediv__', '__subclasshook__', '__doc__', '__radd__', '__or__', '__pow__', '__trunc__', '__rrshift__', '__delattr__', '__reduce__', '__rlshift__', 'conjugate', '__xor__', '__rtruediv__', '__rfloordiv__', '__ge__', '__setattr__', '__class__', 'bit_length', '__neg__', '__mod__', '__int__', '__pos__', 'from_bytes', '__format__', '__rmul__', '__lshift__', '__rsub__', '__new__', '__add__', '__floordiv__', 'imag', 'to_bytes', 'numerator', '__dir__', '__abs__', '__init__', '__sizeof__', '__getnewargs__', '__getattribute__', '__invert__', '__gt__', '__rshift__', '__ne__', '__rdivmod__', '__mul__', '__and__', '__sub__', '__rmod__', '__round__', '__ror__', '__le__', '__eq__', '__float__', '__bool__', '__str__', 'denominator']
>>> dir(2)
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']

4 个答案:

答案 0 :(得分:8)

dir在内部调用__dir__

In [1]: class Hello():
   ...:     def __dir__(self):
   ...:         return [1,2,3]
   ...:     

In [2]: dir(Hello())
Out[2]: [1, 2, 3]

答案 1 :(得分:6)

docs解释一下:

If the object has a method named __dir__(), this method will be called and must
return the list of attributes. This allows objects that implement a custom
__getattr__() or __getattribute__() function to customize the way dir() reports
their attributes.

If the object does not provide __dir__(), the function tries its best to gather
information from the object’s __dict__ attribute, if defined, and from its type
object. The resulting list is not necessarily complete, and may be inaccurate
when the object has a custom __getattr__().

答案 2 :(得分:2)

如果存在,

dir调用__dir__方法, 来自python documentation

  

dir([object])¶没有参数,返回名称列表   当前的本地范围。使用参数,尝试返回列表   该对象的有效属性。

     

如果对象具有名为__dir__()的方法,则将调用此方法   并且必须返回属性列表。这允许对象   实现自定义__getattr__()__getattribute__()功能   自定义dir()报告其属性的方式。

     

如果对象未提供__dir__(),则该函数会尝试最佳   从对象的__dict__属性收集信息,如果   定义,并从其类型对象。结果列表不是   必须完成,并且当对象具有时可能不准确   自定义__getattr__()

答案 3 :(得分:0)

值得指出的是,许多对象上的__dir__无法自定义,因此如果您直接调用它,则无法在其中放置填充/包装。

如果需要,可以轻松替换内置dir并赋予它一些特殊功能。这样的技巧在调试时非常有用。