You'd have already found out by my usage of terminology that I'm a python n00b.
straight forward question:
How can i see a list of methods for a particular object in an interactive python shell like i can in ruby (you can do that in ruby irb with a '.methods' after the object)?
For an enhanced version of
dir()
check outsee()
!You can get it here:
Existing answers do a good job of showing you how to get the ATTRIBUTES of an object, but do not precisely answer the question you posed -- how to get the METHODS of an object. Python objects have a unified namespace (differently from Ruby, where methods and attributes use different namespaces). Consider for example:
((output split for readability)).
As you see, this is giving you the names of all attributes -- including plenty of special methods that are just inherited from
object
, special data attributes such as__class__
,__dict__
and__doc__
, per-instance data attributes (val
), per-instance executable attributes (lam
), as well as actual methods.If and when you need to be more selective, try:
Standard library module
inspect
is the best way to do introspection in Python: it builds on top of the built-in introspection hooks (such asdir
and more advanced ones) to offer you useful, rich, and simple introspection services. Here, for example, you see that only instance and class methods specifically designed by this class are shown -- not static methods, not instance attributes whether callable or not, not special methods inherited fromobject
. If your selectivity needs are slightly different, it's easy to build your own tweaked version ofismethod
and pass it as the second argument ofgetmembers
, to tailor the results to your precise, exact needs.Others have mentioned
dir
. Let me make an remark of caution: Python objects may have a__getattr__
method defined which is called when one attempts to call an undefined method on said object. Obviouslydir
does not list all those (infinitely many) method names. Some libraries make explicit use of this feature, e.g. PLY (Python Lex-Yacc).Example:
Do this:
Python supports tab completion as well. I prefer my python prompt clean (so no thanks to IPython), but with tab completion.
Setup in .bashrc or similar:
Put this in .pythonrc:
It will print "Enabling tab completion" each time the python prompt starts up, because it's better to be explicit. This won't interfere with execution of python scripts and programs.
Example: