Given a python object of any kind, is there an easy way to get a list of all methods that this object has? Or, if this is not possible, at least an easy way to check if it has a particular method other than simply checking if an error occurs when the method is called.
-
It appears you can use this code, replacing 'object' with the object you're interested in:-
[method for method in dir(object) if callable(getattr(object, method))]I discovered it at this site, hopefully that should provide some further detail!
From kronoz -
You can use the built in dir() function to get a list of all the attributes a module has. Try this at the command line to see how it works.
>>> import moduleName >>> dir(moduleName)Also, you can use the hasattr(module_name, "attr_name") function to find out if a module has a specific attribute.
See the Guide to Python introspection for more information.
From Bill the Lizard -
to check if it has a particular method:
try: hasattr(object,"method")
-
On top of the more direct answers, I'd be remiss if I didn't mention iPython. Hit 'tab' to see the available methods, with autocompletion.
And once you've found a method, try help(object.method) to see the pydocs, method signature, etc.
Ahh... REPL.
From jmanning2k
0 comments:
Post a Comment