0

I'm trying to understand few methods while using the dir function..

For example while I execute the following command, it lists the following methods. Any method that starts with __ is a builtin method for int object, but how do I use the other functions like bit_length, conjugate, in case of real I use it as i.real and for bit_lenghth I use it as i.bit_length().

How do I identify when to use as a attribute (real) and when to use as method call (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']
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user1050619
  • 19,822
  • 85
  • 237
  • 413
  • Usually by reading the documentation; calling something that might be a method without knowing what it's supposed to do sounds like a bad idea in any case. `help(i.bit_length)` or `help(i)`. – Wooble May 01 '13 at 14:18

3 Answers3

5

Methods are attributes too. They are just callable attributes.

You can test if something is callable using the builtin callable() function:

>>> 1 .bit_length
<built-in method bit_length of int object at 0x7fe7b2c13118>
>>> callable(1 .bit_length)
True
>>> callable(1 .real)
False
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I think you might want to mention that classes are callable as well, but calling the class results in constructing an instance of the class. :) – J0HN May 01 '13 at 14:03
  • +1, I did't know [`callable()`](http://docs.python.org/2/library/functions.html#callable). Docs: *"class instances are callable if they have a __call__() method."* – Jakub M. May 01 '13 at 14:03
  • 1
    @J0HN: `int` is callable too, so are all 'factories'. Meta classes are callable, they produce new classes, etc. And when called they produce return a new object, but so can many functions. :-) – Martijn Pieters May 01 '13 at 14:07
1

You can check if something is a function using hasattr

hasattr(something, '__call__')
Jakub M.
  • 32,471
  • 48
  • 110
  • 179
0

Methods has __call__ attribute, so in order to determine if something is callable use:

hasattr(val, '__call__')

The problem is that methods can has arguments, so you'll have to inspect the parameter list (e.g. using something like in Python list function argument names SO question)

Community
  • 1
  • 1
J0HN
  • 26,063
  • 5
  • 54
  • 85