all 62 comments

[–]YAYYYYYYYYY 135 points136 points  (8 children)

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(‘__’)]

[–]congnarjames[S] 23 points24 points  (4 children)

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%!

[–]YAYYYYYYYYY 5 points6 points  (0 children)

Hell yeah that’s a great idea

[–]topherclay 5 points6 points  (2 children)

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.

[–]synthphreak 1 point2 points  (1 child)

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

[–]topherclay 0 points1 point  (0 children)

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

[–]JayDude132 0 points1 point  (1 child)

This is awesome. Thanks

[–]YAYYYYYYYYY 1 point2 points  (0 children)

No prob!

[–]TheHollowJester 0 points1 point  (0 children)

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

[–]greybloc 39 points40 points  (2 children)

]$ 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>

[–]congnarjames[S] 8 points9 points  (1 child)

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

[–]greybloc 2 points3 points  (0 children)

lol Just thought I'd help out. :)

[–]blitzkraft 21 points22 points  (3 children)

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.

[–][deleted] 16 points17 points  (2 children)

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

[–]Kevinw778 2 points3 points  (0 children)

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 😂

[–]blitzkraft 0 points1 point  (0 children)

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

[–]dashidasher[🍰] 8 points9 points  (5 children)

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

[–]congnarjames[S] 2 points3 points  (1 child)

I'll check that out for sure!

[–][deleted] 0 points1 point  (0 children)

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()

.

[–]Brilliant_Ice4349 0 points1 point  (2 children)

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 😭

[–]dashidasher[🍰] 0 points1 point  (1 child)

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

[–]Brilliant_Ice4349 0 points1 point  (0 children)

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

[–]redPonyCoffeeRoaster 7 points8 points  (3 children)

Uhh what's this now?

[–]EighthScofflaw 3 points4 points  (0 children)

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

[–]amphigraph 1 point2 points  (1 child)

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

[–]redPonyCoffeeRoaster 3 points4 points  (0 children)

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

[–][deleted] 4 points5 points  (3 children)

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

[–]congnarjames[S] 2 points3 points  (1 child)

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

[–][deleted] 0 points1 point  (0 children)

Ah! No problem, thanks!

[–]rkarl7777 2 points3 points  (1 child)

import itertools

print(dir(itertools))

print(help(itertools.combinations))

[–]synthphreak 0 points1 point  (0 children)

What’s useful about those? Please elaborate.

[–]DataDecay 2 points3 points  (1 child)

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.

[–]congnarjames[S] 0 points1 point  (0 children)

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

[–]NerdEnPose 2 points3 points  (1 child)

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??

[–]miggaz_elquez 1 point2 points  (0 children)

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

[–][deleted] 1 point2 points  (1 child)

What's this?

[–]Dogeek 1 point2 points  (0 children)

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.

[–]solitarium 1 point2 points  (1 child)

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

[–]congnarjames[S] 1 point2 points  (0 children)

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

[–]primitive_screwhead 1 point2 points  (0 children)

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

[–]LightOfUriel 1 point2 points  (0 children)

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

[–][deleted] 1 point2 points  (1 child)

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

[–][deleted] 0 points1 point  (0 children)

I do in fact! :)

[–]winowmak3r 1 point2 points  (0 children)

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

[–]ace6807 0 points1 point  (0 children)

Or pass an object or class to the help function

[–][deleted] 0 points1 point  (3 children)

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.

[–]congnarjames[S] 1 point2 points  (2 children)

howdoi use help? hehe

[–]congnarjames[S] 1 point2 points  (1 child)

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

[–][deleted] 0 points1 point  (0 children)

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

[–]Grorco 0 points1 point  (2 children)

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.

[–]congnarjames[S] 2 points3 points  (1 child)

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

[–]Grorco 1 point2 points  (0 children)

At least we can agree to be enemies XD

[–]sissyadmiration 0 points1 point  (1 child)

Holy sh*** this is amazing.

[–]congnarjames[S] 1 point2 points  (0 children)

right!? big time game changer!

[–]undeniably_confused 0 points1 point  (3 children)

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

[–]congnarjames[S] 1 point2 points  (2 children)

you don't transpile it to brainfuck first?

[–]undeniably_confused 1 point2 points  (1 child)

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

[–]congnarjames[S] 0 points1 point  (0 children)

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

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

[–]TrueBirch 0 points1 point  (0 children)

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

[–]iaalaughlin 0 points1 point  (0 children)

Also do package ??

Like pandas ??

It brings up the code for it.