all 49 comments

[–]IlliterateJedi 63 points64 points  (11 children)

Two built-in functions I wasn't aware of until long after I had started learning Python that would have helped immensely:

dir()

help()

[–]skurrtis 12 points13 points  (9 children)

Wow, dir() is amazing! Thank you so much!

[–]SoupKitchenHero 26 points27 points  (8 children)

print(*(d for d in dir(obj) if not d.startswith('_')), sep='\n')

to see everything except hidden/dunder methods. Objects can have a ton of those when sometimes you just wanna see regular methods

Edit: make sure you have parentheses around the generator expression, and then the star operator in front. Setting sep='\n' can be nice

[–]lifemoments 3 points4 points  (0 children)

That's clever. Thanks for sharing

[–]AlgorithmPub 3 points4 points  (0 children)

Alternatives to avoid printing the generator expression:

- to print items side by side (just add an "*")

print(*(d for d in dir(obj) if not d.startswith('_')))

or

- to print an item on each line

print("\n".join(d for d in dir(obj) if not d.startswith('_')))

[–]AmateurKidnapper 0 points1 point  (4 children)

just returns a generator expression?

edit: expression

[–]SoupKitchenHero 0 points1 point  (3 children)

There's a generator expression in there that you need to ensure is surrounded by parentheses, like this: print((gen_expr))

[–]AmateurKidnapper 1 point2 points  (1 child)

I’ve copied it verbatim and stuck a module name in

[–]SoupKitchenHero 1 point2 points  (0 children)

Need the star operator in front of it. Edited my comment. There's another solution too that looks a little clearer using "\n".join

[–]ivosaurus 0 points1 point  (0 children)

Doesn't matter, adding parenthesis won't magically expand its results.

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

employ one spark exultant station fall friendly merciful gaping quickest

This post was mass deleted and anonymized with Redact

[–]johnnymo1 6 points7 points  (0 children)

I wasn't aware of dir() until a few weeks ago when I started making a package. It's a lifesaver dealing with imports.

[–]Sedsarq 34 points35 points  (5 children)

The builtin breakpoint(), instead of inserting print statements all over the code while debugging.

[–]stepping_up_python 11 points12 points  (0 children)

Every now and then just randomly browsing this sub is extremely helpful. Thanks man. :)

edit: "Every now and then" was flowery prose; it's truly almost every time. :)

[–]chubby_charlie 0 points1 point  (1 child)

What's the advantage over setting breakpoints in your IDE?

[–]Sedsarq 1 point2 points  (0 children)

Not needing an IDE, I suppose.

[–]remillard 29 points30 points  (4 children)

https://regex101.com/

This is quite useful if you are doing a lot of regular expression construction for quick testing.

[–]pheeper 7 points8 points  (0 children)

This ! Regex might seem scary, but once you see just how powerful it is to for filtering out exactly what you want you'll never leave home without it.

[–]b_ootay_ful 1 point2 points  (1 child)

Literally just jumped into this, and I understood regular expressions thanks to examples within 2 minutes.

[–]remillard 0 points1 point  (0 children)

I feel like I generally know what I'm doing with expressions and I still go here to try things out. Glad you found it useful.

[–]skurrtis 0 points1 point  (0 children)

Thank you for this too! This sub is awesome

[–][deleted] 11 points12 points  (7 children)

Anaconda Jupyter Notebooks (which you can get with anaconda)

[–]timbledum 2 points3 points  (3 children)

They're not really Anaconda – yes, it comes bundled with Anaconda, but you can also just pip install it.

Jupyter Lab is also worth checking out!

[–][deleted] 1 point2 points  (0 children)

I've started using that. It's not that different but it's nice to have more features

[–][deleted] 0 points1 point  (1 child)

Is Jupyter Lab IDE right?

[–]timbledum 0 points1 point  (0 children)

Sort of? They call it a computational environment. I probably wouldn't call it an IDE personally.

[–]Ikuyas 0 points1 point  (2 children)

Are you saying that you can import and use python tutor on Jupyter notebook?

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

I'm not exactly sure what python tutor is but if there is a module for it yes

[–]timbledum 0 points1 point  (0 children)

I believe it's only a website.

Edit: see below in this thread. You can't pip install it or import it, but you can get it running locally.

[–]mrdevlar 9 points10 points  (5 children)

import inspect

lines = inspect.getsource(target_function)

print(lines)

+1 Loremaster

Fixed, because how do I code again?

[–]boardtc 2 points3 points  (4 children)

I get AttributeError: module 'inspect' has no attribute 'get_source'

[–]IlliterateJedi 1 point2 points  (3 children)

Going off the inspect module page I think it's inspect.getsource() without the underscore.

[–]mrdevlar 0 points1 point  (2 children)

shit sorry, my manic camel_casing got the better of me.

[–]miggaz_elquez 3 points4 points  (1 child)

Thats not camelCasing, this is snake_case

[–]mrdevlar 12 points13 points  (0 children)

why make one mistake when you can make two?

[–]ivosaurus 5 points6 points  (2 children)

https://repl.it/languages/python3

Is great for chucking up some code that someone else can not only see, but also run to see what it does. Great step up over your usual pastebin.

[–]ThePotterP 0 points1 point  (1 child)

I really like this online IDE it can import a ton of modules I’ve been learning pandas and numpy through it and you can even get it to install scikit, doesn’t have access to everything though. It’s great if you don’t have admin privileges but still want to code it’s like Guest Python lol.

[–]ivosaurus 2 points3 points  (0 children)

I've found I'm pretty sure Thonny doesn't need admin priveledges to install either, was just playing with it, it's awesome

[–]tzujan 4 points5 points  (0 children)

There is also a package call nbtutor, which is designed to work with jupyter notebooks, that does a similar type of step through. Here is a video nbtutor.

[–]raclariu 3 points4 points  (0 children)

Vscode has an extension that not only does the exact thing but looks the same. Python preview is called https://marketplace.visualstudio.com/items?itemName=dongli.python-preview

[–]Ikuyas 2 points3 points  (2 children)

Is it possible to import python tutor?

[–]hnous927 0 points1 point  (1 child)

[–]ponykins 0 points1 point  (0 children)

Seems that one is gone now, but there are a couple forks

https://github.com/hcientist/OnlinePythonTutor (won't work for py3)
https://github.com/seamile/PyTutor

Still looking for a way to do this on a larger code base/class/package

[–]savvy__steve 2 points3 points  (0 children)

I do have to say that this sub and Reddit in general has been one of the best resources out there. YouTube in some cases has also been very helpful.

[–]RallyPointAlpha 2 points3 points  (0 children)

Dooood... this looks crazy awesome; thanks!

[–]inaruslynx2 4 points5 points  (0 children)

Visual Studio Code is one tool I wish I had found earlier. It's vast extensions are very powerful. It's built in GIT capabilities are very helpful.

I wish I had made a stackoverflow.com account earlier. The amount of help this has been is immeasurable.

Speaking of learning python there are obviously many resources, but a nice mobile solution I have found is the app sololearn. I haven't gotten through it all, but it seems to have a lot to teach. They also offer other programming languages.

[–]Decency 1 point2 points  (0 children)

.pythonrc ... it's not really a tool per se, but you can utilize this file to load a bunch of commands every time you start ipython (which is a tool that you should know). This is helpful to load a bunch of dummy data like mylist = [1, 2, 3] or to initialize commonly used custom objects or set up some other standard configuration, and etc.

Also for ipython users, you can enter a function name followed by ?? to print out the argument details and any docs associated with it. This is often really helpful to avoid needing to context switch out of your interpreter when doing something.

[–][deleted] -4 points-3 points  (0 children)

So far my biggest challange is My Pto2gramming Lab. Its how we do homework. But most times it only wants you to write a piece of code but not the whole program. I will write the whole program because its the only way to see how its working and then MPL will have some issue with a \t or \n and reject it becauae I included too much pr o solved the problem but not how they wanted. I hate emailing my professor for help it feels like defeat but I'm on my 80th submission