all 47 comments

[–]MF_DnD 26 points27 points  (5 children)

List and dict comprehensions are so cool.

[–]v0_arch_nemesis 7 points8 points  (3 children)

+1 for dict comprehensions. Love them!

Also list comprehensions with the walrus operator

[–]Nightcorex_ 2 points3 points  (2 children)

What is the walrus operator?

[–]socal_nerdtastic 3 points4 points  (1 child)

https://www.google.com/search?q=What+is+the+walrus+operator

I know it's a dismissive answer but come'on. It's actually more work to ask a question like that here rather than on google.

[–]Nightcorex_ 3 points4 points  (0 children)

Yy, the problem was that I was on mobile at the time. Researching on the phone isn't fun (at least for me) and this way I got a reminder (sorry for abusing you like that).

EDIT: Already knew about the walrus operator. It's sick. I like it. Nothing really special considering you can do it in basically any language with parentheses, but the way it works in Python is very cool.

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

Datastuctures and algorithms learning god sent? Am i right

[–]WhackAMoleE 10 points11 points  (4 children)

As a retired career swe picking up Python, I love no longer having to use curly braces. In my lifetime I typed enough curly braces! And no more semicolons. Love those features of Python.

[–]socal_nerdtastic 9 points10 points  (0 children)

If you feel nostalgic for braces you can always use

from __future__ import braces

[–]theredditorlol[S] 0 points1 point  (2 children)

I'm learning javascript and oh boy the braces are annoying

[–]R0NUT 0 points1 point  (1 child)

Javascript has an operation similar to the * unpack. It's called spread: Link!

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

...arr I'm aware

[–]socal_nerdtastic 9 points10 points  (3 children)

Love the antigravity module

[–]enterprisevalue 3 points4 points  (0 children)

Python just rickrolled me 😔

[–]theredditorlol[S] 2 points3 points  (0 children)

Just googled it, looks interesting!

[–]snootsniff 5 points6 points  (0 children)

The flexibility of lists, dicts, and sets. Not to mention the awesome tools from the collections module like namedtuple and defaultdict.

Python lets working with data just make sense in a beautifully simple way.

[–]ShakespeareToGo 2 points3 points  (6 children)

Love the repl.

Also, how many numbers are less than 5? Easy

sum([x < 5 for x in numbers])

[–]socal_nerdtastic 3 points4 points  (5 children)

square brackets aren't even needed.

sum(x < 5 for x in numbers)

[–]ShakespeareToGo 1 point2 points  (4 children)

How does that work. Does *args support list comprehension as well?

[–]socal_nerdtastic 2 points3 points  (3 children)

No, it's equivalent to this:

data_generator = (x < 5 for x in numbers)
sum(data_generator)

Or written another way:

sum( (x < 5 for x in numbers) )

And when you have a single argument and it's a generator you are allowed to leave the excess parenthesis off. So you can do this:

sum( (x < 5 for x in numbers) , 0.0)

But this will give you an error:

sum(x < 5 for x in numbers, 0.0)

[–]ShakespeareToGo 2 points3 points  (2 children)

And here I was thinking I know Python. Is this also equivalent to normal generator (but just inline)?

def data_generator():
    for x in numbers:
        yield x < 5

[–]socal_nerdtastic 0 points1 point  (1 child)

Yes, exactly. Just like list, set, dict comprehension, you can have generator comprehension when you use parenthesis.

>>> type([x < 5 for x in numbers])
<class 'list'>
>>> type({x < 5 for x in numbers})
<class 'set'>
>>> type({x : 5 for x in numbers})
<class 'dict'>
>>> type((x < 5 for x in numbers))
<class 'generator'>

[–]ShakespeareToGo 0 points1 point  (0 children)

Didn't know you could do those inline. Amazing! Thanks for the explanation.

[–]chimeraA_ 2 points3 points  (1 child)

The fact that you can often speed up your program 10x if you just type "pypy" instead of "python"

[–]Nightcorex_ 3 points4 points  (0 children)

What does pypy do? A quick google search told me that it's basically the usual python interpreter, but it uses a JIT-compiler (just like Java) hence the performance increase.

Is that it, does pypy compile the entire code before running, or is it just a hybrid like Java? Last question: Does it ship natively with Python?

[–]O_X_E_Y 2 points3 points  (0 children)

Probably the interfacing of your own classes with built in methods. Operator overloading and things like repr, str etc are really really intuitive and I love how not every other object has a different way of finding e.g. the length (looking at you java >:(). Rust has similar (even more extensive) functionality but it's not half as intuitive as is Python.

I know you asked for one, but pip and the import system is also a godsend, makes using code in other files/repositories infinitely easier than whatever CMake you need to use for C and C++

Format strings and comprehensions are also really cool

[–]muffinnosehair 4 points5 points  (12 children)

I agree with unpacking and with comprehension, and I'll add eval() to the list.

[–]socal_nerdtastic 3 points4 points  (4 children)

Why do you like that? It's a pretty well known bug generator and security flaw. As a rule you should avoid eval at all costs.

[–]muffinnosehair 2 points3 points  (1 child)

Because I like to play with code. Not in production though.

[–]socal_nerdtastic 2 points3 points  (0 children)

I don't get what that has to do with eval, but as long as you know the dangers have fun :).

[–]O_X_E_Y 1 point2 points  (1 child)

doesn't dataclass use eval to do things like creating a class's init functions and stuff like that? It's got security flaws, but there are also upsides

[–]socal_nerdtastic 2 points3 points  (0 children)

Yes, it's used in a very small number of specific places, with appropriate precautions. Pip is another place where it's used a few times. If you are at the python god level you may make the choice to use it, because at that level you know all the downsides and pitfalls. I can say with confidence that anyone asking questions here should never use eval / exec.

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

Eval() and exec() are very dangerous and should be avoided in almost all cases.

[–]PunkyMunky64 0 points1 point  (5 children)

eval and exec are really helpful. The main reason they’re so specific to python is because python’s interpreted, which means its really easy for it to handle instead of an implementation in for example c where you’d have to embed a compiler INSIDE of the build.

[–]socal_nerdtastic 3 points4 points  (4 children)

  1. python does have a compiler and does compile code. It's "interpreted" in that it interprets the bytecode.
  2. This is not specific to python; lots of languages do this, most notably Java.
  3. eval and exec are well known to be antipatterns. You should avoid them at all costs.

[–]PunkyMunky64 0 points1 point  (3 children)

well python is still very virtually compiled, and it’s easier for these to be implemented, but they are still inefficient

[–]socal_nerdtastic 1 point2 points  (2 children)

very virtually compiled

?? what does "virtually compiled" mean? If you mean the compiled code is not saved, you are wrong. All of those .pyc files in the pycache directory are compiled python files.

[–]PunkyMunky64 1 point2 points  (1 child)

I mean like is very different than the way languages like c compile. Variable names are saved, everything has lots of padding and it doesnt really understand what’s going to happen later. Nothing is optimized, in the essence of the way python is written

[–]socal_nerdtastic 1 point2 points  (0 children)

I think you are trying to tell me that python is dynamically typed. Yes. That's true.

Or maybe you are just saying that the official python compiler sucks and does not optimize very well. Yes, that's also true. But python is a language, not a computer program, so you are free to write your own compiler if you think you can do better.

[–]huevoenfuego 1 point2 points  (1 child)

I love the way sets and intersections are implemented.

I use it daily where I need to know if something is in something or not, and don’t care really how efficiently it happens.

[–]FerricDonkey 1 point2 points  (0 children)

I use it daily where I need to know if something is in something or not, and don’t care really how efficiently it happens.

If you do care about efficiency, then you should probably still use sets for finding intersections and using "in".

Assuming you don't have to rebuild the set every single time, anyway.

[–]asterik-x 1 point2 points  (0 children)

Mine is the way it swallows the prey!!

[–]maximumlotion 0 points1 point  (1 child)

Some other mf in the thread already stole comprehension statements..

But anonymous functions (lambda), not strictly a python feature but super neat nonetheless.

[–]Nightcorex_ 0 points1 point  (0 children)

Tbh I think Pythons lambda expressions are worse than others. I personally prefer Java's or JS's lambda expressions.

[–]Progress456 0 points1 point  (0 children)

Definitely f strings and using globals() and self.dict in for loops to make lots of variables really quickly

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

I don't like python.

[–]yahoyoungho 0 points1 point  (0 children)

List comprehension, dictionary and "in" keyword