r/learnpython • • Oct 27 '19

mind blown using help() in python

I've been programming in python for about a year now. And I've never encountered this module before, and it's soooooooo incredibly useful. Like I'll probably never even google anything ever again ( ok thats total bullshit). But really my point here is if you're learning python you should be aware of and know how to use the help module IMHO. across the thousands of webpages I've been to and resources I've used to teach myself I can't believe I didn't run into it sooner. Anyways I just thought sunday was a good day for ranting and sharing. I hadn't initially planned on asking a question here but maybe we could get something along the lines of things you found out too late and or things everyone learning python IYHO should know. :)

edit:

help is a function in python which describes the object you pass to it. The description it outputs is it's docstring, as pointed out by DataDecay more information on docstrings can be found in PEP-257

so if you want more information about an object you can pass it as an argument to help.

Say for instance you were confused as I currently am about how asyncio works.

You could type help(asyncio) and it would give you information about that package (technically object).

YAYYYYYYY and rkarl7777 both mentioned another very useful utility named dir, which will list the methods available to the object passed to it.

So after gaining a general understanding of the base package you can then look through all of its methods.

dir(asyncio)

then if you have questions about any of the methods in that list you can query those with help.

help(asyncio.StreamWriter)

Now hang on just a second while I try and find somebody who is better at explaining things than I am.

I went through a couple of articles, this one seems to be the best.

https://www.linuxnix.com/python-builtin-helpdir-help-type-and-___doc_-functions/

448 Upvotes

62 comments sorted by

View all comments

Show parent comments

24

u/congnarjames Oct 28 '19

wow yeah that is super useful, when I was programming in java it seemed like the IDE did a good job of populating all those methods for you and I havn't gotten around to looking for a good replacement for that. So that bit of info is very appreciated! Take it a step further [help(x) for x in dir(list) if not x.startswith('__')] and at least for me it'll drive down my need to go look at the docs by like 70%!

7

u/topherclay Oct 28 '19

I use pyCharm as a an IDE. If you type Ctrl+q with your curser on any function or method it displays the '''doc string''' from where that function is defined, which I think is the same as what help() does.

It also seems to do a really good job at showing you all the potential methods for tab completion, if that's what you mean by populating methods.

2

u/synthphreak Oct 28 '19

Same with Jupiter Notebooks. I think it’s Shoft + Tab or something? Anyway docstring display functionality is there.

1

u/topherclay Oct 28 '19

That was the first thing I franticly searched for after graduating from fiddling around in Jupyter to baby's first .py script in pycharm.