我試圖瞭解使用dir函數時的幾種方法。Python中的Dir函數
例如,當我執行以下命令時,它列出了以下方法。與__
開頭的方法是int
對象內建的方法,但我怎麼使用其他功能,如bit_length
,conjugate
,在real
情況下,我用它作爲i.real
和bit_lenghth
我用它作爲i.bit_length()
。
如何識別時作爲屬性使用(真正的),當爲方法調用(bit_length()
)使用方法:
>>> i=0
>>>
>>> dir(i)
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delat
tr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__forma
t__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__',
'__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul
__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow
__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_
ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ro
r__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rx
or__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '_
_truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', '
imag', 'numerator', 'real']
通常通過讀取文件;在任何情況下調用可能是方法的東西而不知道它應該做什麼聽起來都是個壞主意。 'help(i.bit_length)'或'help(i)'。 – geoffspear 2013-05-01 14:18:02