Python – Dir and Help usage

One of the best ways to understand Python is using dir and help.

Dir can do a couple of neat things, like returning local scope names.

>>> dir()

[‘__builtins__’, ‘__doc__’, ‘__file__’, ‘__name__’, ‘__package__’]

Dir can be used with an argument.

>>> dir(__name__)

['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', 
'__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', 
'__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', 
'__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', 
'__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', 
'__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 
'capitalize', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 
'format', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit',
'isidentifier''islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 
'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 
'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip',
'split', 'splitlines','startswith', 'strip', 'swapcase', 'title', 'translate', 
'upper', 'zfill']

 

>>> dir(__name__.__getattribute__)

['__call__', '__class__', '__delattr__', '__doc__', '__eq__', '__format__',
 '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__',
 '__lt__', '__name__', '__ne__', '__new__', '__objclass__', '__reduce__',
 '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__',
 '__str__', '__subclasshook__']

>>> dir(__name__.__getattribute__.__getattribute__)

call__', '__class__', '__delattr__', '__doc__', '__eq__', '__format__',
'__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__',
'__lt__', '__name__', '__ne__', '__new__', '__objclass__', '__reduce__',
'__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__']

Arguments can be passed into the help command.

>>> help(__name__)

Help on built-in module __main__:

NAME
    __main__

FILE
    /home/Rzz4Nf/prog.py

>>> help(__name__.__getattribute__)

Help on method-wrapper object: __getattribute__ = class method-wrapper(object) | Methods defined here: | | __call__(...) | x.__call__(...) <==> x(...) | | __eq__(...) | x.__eq__(y) <==> x==y | | __ge__(...) | x.__ge__(y) <==> x>=y | | __getattribute__(...) | x.__getattribute__('name') <==> x.name | | __gt__(...) | x.__gt__(y) <==> x>y | | __hash__(...) | x.__hash__() <==> hash(x) | | __le__(...) | x.__le__(y) <==> x<=y | | __lt__(...) | x.__lt__(y) <==> x<y | | __ne__(...) | x.__ne__(y) <==> x!=y | | __repr__(...) | x.__repr__() <==> repr(x) | | ---------------------------------------------------------------------- | Data descriptors defined here: | | __objclass__ | | __self__

>>> help(__name__.__getattribute__.__sizeof__)

Help on built-in function __sizeof__:

__sizeof__(...)
    __sizeof__() -> size of object in memory, in bytes
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s