Show methods for any object in Python

Posted in python

This is pretty simple, but it took me a few years before I ran into this. It’s just not covered in the basic documentation. Forget auto completion for a moment and let’s just show how to display the methods available for an object, any object.

>>> import subprocess
>>> p = subprocess.Popen(["cat file.conf"], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

But I want to know what methods this object has without having to look it up…

>>> p.__dict__

{’_child_created’: True, ‘returncode’: None, ’stdout’: ‘, mode ‘rb’ at 0xb765a2f0>, ’stdin’: ‘, mode ‘wb’ at 0xb765a250>, ‘pid’: 1876, ’stderr’: ‘, mode ‘rb’ at 0xb765a340>, ‘universal_newlines’: False}

>>> p.__dict__.keys()

['_child_created', 'returncode', 'stdout', 'stdin', 'pid', 'stderr', 'universal_newlines']

>>> import sys
>>> sys.__dict__.keys()
['setrecursionlimit', 'dont_write_bytecode', 'getrefcount', 'path_importer_cache', 'stdout', 'getprofile', '__stdin__', 'version_info', 'exc_clear', 'prefix', 'getfilesystemencoding', 'byteorder', '_clear_type_cache', 'excepthook', 'subversion', 'exc_type', 'ps1', '__excepthook__', 'executable', 'float_info', 'copyright', 'setdlopenflags', 'exec_prefix', 'getdlopenflags', 'getrecursionlimit', 'py3kwarning', 'path_hooks', '__package__', '_current_frames', 'platform', 'maxsize', 'version', 'exit', 'call_tracing', 'callstats', 'flags', 'setcheckinterval', '__doc__', 'api_version', '__plen', 'getdefaultencoding', 'getcheckinterval', 'maxunicode', 'settrace', 'setprofile', 'argv', '__stdout__', 'meta_path', '__name__', 'builtin_module_names', 'stdin', '__stderr__', '__egginsert', 'displayhook', 'ps2', 'gettrace', 'modules', 'warnoptions', 'last_type', 'getsizeof', 'last_traceback', 'maxint', '__displayhook__', '_getframe', 'stderr', 'exc_info', 'path', 'last_value', 'hexversion']

Posted by admica   @   15 April 2010
Tags : , , ,

Related Posts

Like this post? Share it!

Digg Twitter StumbleUpon Delicious Technorati Facebook RSS

1 Comments

Comments
Apr 25, 2010
8:48 am
#1 Bill Totman :

Thanks for the post - it took a few Google searches, but I finally found what I was looking for.

Leave a Comment

Name

Email

Website

Previous Post
« Microsoft’s Slave Labor at KYE factory in Dongguan China
Next Post
Split a dictionary into two lists or vice versa in Python »
Powered by Wordpress   |   Lunated designed by ZenVerse