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

136

u/YAYYYYYYYYY Oct 28 '19

Another useful tool is dir(). It gives a list of all the methods you can call on an object. It really comes in handy instead of trying to memorize all the string and list methods, just type dir(list) or dir(str).

To make it even nicer, use a list comprehension to filter out the unnecessary internals: [x for x in dir(list) if not x.startswith(‘__’)]

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/YAYYYYYYYYY Oct 28 '19

Hell yeah that’s a great idea

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.

1

u/JayDude132 Oct 28 '19

This is awesome. Thanks

1

u/[deleted] Oct 28 '19

And since we're at the topic, foo.__dict__is helpful for debugging and exploratory-surgery type work (regardless of it's limitations).

37

u/greybloc Oct 27 '19
]$ python
Python 3.7.4 (default, Oct  4 2019, 06:57:26) 
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> help()

Welcome to Python 3.7's help utility!

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at https://docs.python.org/3.7/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics".  Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".

help>

8

u/congnarjames Oct 27 '19

a picture is worth a thousand words or well like ~120 as the case may be haha

3

u/greybloc Oct 27 '19

lol Just thought I'd help out. :)

8

u/dashidasher Oct 28 '19 edited Oct 28 '19

One of the things I use almost every day is the Python Debugger https://docs.python.org/3/library/pdb.html. If you put pdb.set_trace() anywhere in your code it will stop executing the script at that point and let you play around untill you use the continue command (you can also do next, step, untill...). I've only found out about it 2 months into my job working with python. Been using python for like 3 years before that too. All that time I've only used prints to debug xD

3

u/congnarjames Oct 28 '19

I'll check that out for sure!

1

u/[deleted] Oct 29 '19

Make things easier on yourself.

New in version 3.7: The built-in

breakpoint()

, when called with defaults, can be used instead of

import pdb; pdb.set_trace()

.

1

u/Brilliant_Ice4349 Dec 14 '25

Omg you're a lifesaver, whenever I'm defining a function for a piece of code later one, which I know won't work until I finish defining all the functions required for it, I had to copy paste each defining function + global variables required for it to work on another notebook 😭

1

u/dashidasher Dec 14 '25

Glad to hear. This comment is a bit outdated so make sure to use breakpoint() instead of pdb.set_trace() as one of the replies mentions :D

1

u/Brilliant_Ice4349 Dec 14 '25

Thanks :)
cookie += 1
Print(f"Here you have {cookie} cookie :)")

4

u/[deleted] Oct 28 '19

Can someone explain what it is and how to use it? For the love of God please!

3

u/congnarjames Oct 28 '19

hahaha yeah for sure man sorry, I got sort of excited and kind of glazed over things there. I'll edit my post

1

u/[deleted] Oct 28 '19

Ah! No problem, thanks!

3

u/DataDecay Oct 28 '19

Maybe it's because I came from the ops side before development. But the first thing I did in python was locate a man page, or as you have just discovered, the help function. Fun fact, PEP-257 offers some guidance on docstrings, for which the help function outputs.

1

u/congnarjames Oct 28 '19

I haven't spent much time digging through the PEPs I'll have to check it out one of these days

2

u/solitarium Oct 28 '19

I've rarely ever code live on the interpreter. I think I have a reason to do so now.

2

u/congnarjames Oct 28 '19

I usually keep one going while I'm coding it's nice to run little tests and one offs!

23

u/blitzkraft Oct 28 '19 edited Oct 28 '19

help() only works where the devs put in the extra work for documenting. It pulls in the docstring and you can write it too!!

Say you have a function, then add the docstring like this:

def hello(name='human'):
  """
  Says hello, optionally takes a name
  """
  print("Hello, {}".format(name))

Now, if you import this, and call help(hello), you will see the help string.

16

u/[deleted] Oct 28 '19

Documenting is not “extra work,” but rather essential work that is too often considered drudgery.

3

u/Kevinw778 Oct 28 '19

Agreed. Especially in languages like Python and Javascript, uncommented / undocumented code is an absolute nightmare. It's ok though, people will realize their mistake when they come back to their own code at some point hahah. "Nobody else will use this, it's fine."

Programmers: The largest group of people that don't consider themselves people, apparently 😂

1

u/blitzkraft Oct 29 '19

Yes. I feel I ratted myself out for not documenting well (calling it "extra work").

8

u/redPonyCoffeeRoaster Oct 27 '19

Uhh what's this now?

4

u/EighthScofflaw Oct 28 '19

Try calling help(thing), where thing is anything in the namespace.

2

u/amphigraph Oct 27 '19

you can pass a function, method, class (or instance of a class), etc. to help() and it returns the docstring

4

u/redPonyCoffeeRoaster Oct 27 '19

Awesome. New snake charmer here. I'll check it out.

1

u/[deleted] Oct 28 '19

I've been learning Python in the last year too and never knew of that module. I'll have to try it out tomorrow when I have more time. Reminds me of the howdoi module a bit.

2

u/congnarjames Oct 28 '19

howdoi use help? hehe

2

u/congnarjames Oct 28 '19

nvm that didn't work well, still seems like a good thing to have around though!

1

u/[deleted] Oct 28 '19

Lol, I was going to try that. Siri came through for me though. :)

1

u/Grorco Oct 28 '19

I love in vim with jedi-vim when I use alt-tab to complete it shows this at the top of the screen. So I can read the description while going through the stuff in the drop-down list.

3

u/congnarjames Oct 28 '19

That does sound cool. but we are in the midst of a holy war and i friend am on the other side of it. ;)

2

u/Grorco Oct 28 '19

At least we can agree to be enemies XD

1

u/sissyadmiration Oct 28 '19

Holy sh*** this is amazing.

2

u/congnarjames Oct 28 '19

right!? big time game changer!

1

u/undeniably_confused Oct 28 '19 edited Oct 28 '19

I write my pseudo code in a assembly, than convert that to python as I read it

2

u/congnarjames Oct 28 '19

you don't transpile it to brainfuck first?

2

u/undeniably_confused Oct 28 '19 edited Oct 28 '19

How could I transpile it it's pseudo code, this is evidently how I code always

1

u/congnarjames Oct 28 '19

I'm pretty sure you just do something like this.

https://www.youtube.com/watch?v=eMJk4y9NGvE

3

u/rkarl7777 Oct 28 '19

import itertools

print(dir(itertools))

print(help(itertools.combinations))

1

u/synthphreak Oct 28 '19

What’s useful about those? Please elaborate.

3

u/NerdEnPose Oct 28 '19

In the iPython console you can just type object? And it'll do the same thing. object?? Gives you the python code too.

Example:

from pathlib import Path

Path? Path??

2

u/miggaz_elquez Oct 28 '19

Very useful, and more quick to read than the help function

2

u/[deleted] Oct 27 '19

What's this?

2

u/Dogeek Expert - 3.9.1 Oct 28 '19

He is talking about the built-in help function, which prints out the docstring of whatever object you pass in, whether it's a class, a metaclass, an instance, a function or a module. Another useful function to know is dir which prints out all the methods and attributes of a particular object.

2

u/primitive_screwhead Oct 28 '19

Also worth knowing "pydoc", which is the command line version of Python's help().

2

u/LightOfUriel Oct 28 '19

It seems to be exactly same output as intellisense provides in good IDE at few times more work.

2

u/[deleted] Oct 28 '19

Then you'll love this article for more Python built-ins.

1

u/[deleted] Oct 28 '19

I do in fact! :)

2

u/winowmak3r Oct 28 '19

Holy shit this is really useful. I'm still learning, thanks for pointing out this out!

1

u/ace6807 Ace6807 Oct 27 '19

Or pass an object or class to the help function

1

u/TrueBirch Oct 28 '19

It's so useful! Just like the question mark in R.

1

u/iaalaughlin Oct 28 '19

Also do package ??

Like pandas ??

It brings up the code for it.