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']
1
  • 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). Commented May 1, 2013 at 14:18

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

3 Comments

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. :)
+1, I did't know callable(). Docs: "class instances are callable if they have a __call__() method."
@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. :-)
1

You can check if something is a function using hasattr

hasattr(something, '__call__')

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.