[Python Tips] Getting Help
There are a few handy functions built into Python to help the developer get more information about an object.
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.
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
- f-strings in Python 3.6
- Underscores in numeric literals
- A better interactive shell
- Secrets Module - New in 3.6
- PEP 8
- Named Tuples
- Type Annotation
- Jupyter Notebooks
Witness & Administrator of four full nodes
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