[Python Tips] Getting Help

in #programming7 years ago

There are a few handy functions built into Python to help the developer get more information about an object.

help()

If you want to get the documentation on an object or function you can use the help() function to look up any documentation available.

> help(id)
Help on built-in function id in module builtins:
>>> 
id(obj, /)
    Return the identity of an object.  
    This is guaranteed to be unique among simultaneously existing objects.
    (CPython uses the object's memory address.)

This is only useful when using python interactively via repl or bpython.

dir()

When you call dir() without any arguments, it will return the names of everything in scope.

>>> dir()
['__builtins__', '__doc__', '__name__', 'help']
>>> a = 5
>>> dir()
['__builtins__', '__doc__', '__name__', 'a', 'help']

If you call dir on an object, it will return all the available attributes.

>>> a = 'Hello World'
>>> dir(a)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init
_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__',
 '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', '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() is also most useful when using python interactively.

While both of these functions are not typically used on a daily basis, knowing they are available can be helpful when working interactively. I typically work interactively when doing querying or working with objects I don't understand yet to get a feel for how they work. This is where bypython shines.

As always, the Python Online Docs is a great reference when trying to understand how the built-in functions and objects work. They can be difficult to understand to new programmers and just doing a Google search for what you need help with and then clicking videos usually yields the best results. Almost every topic has been covered in detail in a YouTube video.

Good Python YouTubers I highly recommend.

If you haven't already, consider opening an account on StackOverflow the most popular Q&A site for developers across the world.

My Python Tips Series

X48EJ

Why you should vote me as witness

Witness & Administrator of four full nodes

themarkymark.png

My recent popular posts

STEEM, STEEM Power, Vests, and Steem Dollars. wtf is this shit?
The truth and lies about 25% curation, why what you know is FAKE NEWS
WTF is a hardware wallet, and why should you have one?
GINABOT - The Secret to your Sanity on Steemit
How to calculate post rewards
Use SSH all the time? Time for a big boy SSH Client
How to change your recovery account
How curation rewards work and how to be a kick ass curator
Markdown 101 - How to make kick ass posts on Steemit
Work ON your business, not in your business! - How to succeed as a small business
You are not entitled to an audience, you need to earn it!
How to properly setup SSH Key Authentication - If you are logging into your server with root, you are doing it wrong!
Building a Portable Game Console

Sort:  

👍👍

Great post,thanks for the information, have a good day dude!

Programming that is difficult for me .

Global Biggest Airdrop
1 GIFT = US$0.2,
100 GIFT by registration,
100 Gift by Telegram,
Gain more 100000 GIFT from any activities,
Click link to get GIFT
Win lottery for iPhoneX BTC ETH GIFT everyday!
Please Use My Referral Link! Thanks

Great stuff as always.

@themarkymartk,I do not comment on what you post here,I really hope for you, how can I remove from blacklist @buildawhale,Since I now never make another mistake in steemit,
And I really hope the comments from @badcontent, never in the paste again in my post.

This is a very helpful resource pointer to good materials for python which I've been experimenting with. Thank you @themarkymark

This is really good! It's also worth knowing that you can also call help without an argument to enter an interactive help menu.