Dir () - how can I distinguish between functions / method and simple attributes?
3 answers
To display a list of specific names in a module, such as a math module and their types, you could do:
[(name,type(getattr(math,name))) for name in dir(math)]
getattr (math, name) returns an object (function or otherwise) from the math module named by the string value in the "name" variable. For example type (getattr (math, 'pi')) is 'float'
+2
source to share
Another way is to use the function getmembers
in inspect
. You can get a similar result for James by one of
from inspect import getmembers
getmembers(obj) # => ...
For more information, please take a look at:
https://docs.python.org/2/library/inspect.html#inspect.getmembers
0
source to share