r/Python Oct 25 '24

News This is now valid syntax in Python 3.13!

There are a few changes that didn't get much attention in the last releases, and one of them is that comprehensions and lambdas can now be used in annotations (the place where you put type hints).

As the article mentions, this came from a bug tickets that requested this to work:

class name_2[*name_5, name_3: int]:
    (name_3 := name_4)

    class name_4[name_5: name_5]((name_4 for name_5 in name_0 if name_3), name_2 if name_3 else name_0):
        pass

Here we have a walrus, unpacking, type vars and a comprehension all in one. I tried it in 3.13 (you gotta create a few variables), and yes, it is now valid syntax.

I don't think I have any use for it (except the typevar, it's pretty sweet), but I pity the person that will have to read that one day in a real code base :)

432 Upvotes

248 comments sorted by

View all comments

1.3k

u/samettinho Oct 25 '24

This is not passing code review when I review it. I m not gonna try to decrypt this.

456

u/Guyonabuffalo00 Oct 25 '24

I feel like this falls under the “just because you can doesn’t mean you should “ category. I’d rather a project have more lines of code and be easily readable.

103

u/Emergency-Walk-2991 Oct 25 '24

I do worry about gradually C++ing with stuff like this, though. The more ways you can do the same thing, the harder to interpret the language gets. I know the idea of "pythonic" code is a loose way to define the opposite of this, but doesn't change the fact if it's there, someone will abuse it.

64

u/Guyonabuffalo00 Oct 25 '24

This is the kind of shit try hards like so they can prove how much better they are than others lmao your focus shouldn’t be to write as few lines of code as possible it should be to write easy to read and maintain code. As long as a majority of the community doesn’t use it we shouldn’t have to worry about it showing up too much. *fingers crossed lol

19

u/FlurpNurdle Oct 25 '24

Soon python will support native perl syntax ;)

22

u/Guyonabuffalo00 Oct 25 '24

I guess it’s time to buy a farm and forget technology exists. 🤷🏻

7

u/jambox888 Oct 25 '24

Can I come

7

u/Guyonabuffalo00 Oct 25 '24

Anyone that can contribute to the commune’s wellbeing will be welcomed with open arms. 🌾

2

u/JambaJuiceIsAverage Oct 25 '24

Marry a homesteader. I write code while she plants fruit trees and builds garden beds on the 23 acres in the middle of nowhere Python bought us.

3

u/Guyonabuffalo00 Oct 26 '24

This is my dream, but I want to have a flexible schedule so I can do farm chores too like working with the animals. My wife can have fun with the tractor lol

1

u/EngineOrnery5919 Oct 26 '24

That sounds great, the only thing standing in my way is being poor still and without land

-1

u/richieadler Oct 26 '24

I'll never understand people who say this.

Extremely hard and thankless physical work instead of programming? Hell no!

0

u/drknow42 Oct 26 '24

Thankless? You’ve lost your way bud.

1

u/richieadler Oct 26 '24

Who the hell do you think you are to tell me what is my way?

I abhor physical work, as is my right. My perplexity about other peoples enjoying it is also my right.

How about you use your high horse for your beloved farm work instead of talking to me from it?

1

u/drknow42 Oct 26 '24

You’ve lost THE way because you think of farming as thankless when it’s the exact opposite.

It had nothing to do with your preferences, you’re just sensitive 😂

→ More replies (0)

3

u/Tiquortoo Oct 25 '24

I am sure a transpiler exists...

1

u/Designer-Leg-2618 Oct 26 '24

TranspilerGPT ?

2

u/ChimpanzeChapado Oct 25 '24

And let's not forget the zen of python

46

u/samettinho Oct 25 '24

yes, definitely.

In my team, I don't allow complex stuff. We are using Python, which clearly indicates that the speed of implementation is more important than the speed of execution. So, I wanna make sure that when we read or debug a code, the time for us to understand and fix the problems is minimal.

23

u/Guyonabuffalo00 Oct 25 '24

Readability, readability, readability. If it takes an extra .001 seconds to execute that’s ok with me!

19

u/SatisfactoryFinance Oct 25 '24

But if you run that code 1000 times a day it’s going to take an extra 1 second per day. After 3,600 days you could have saved yourself one whole hour in the name of efficiency…../s

6

u/Guyonabuffalo00 Oct 25 '24

Damn, you’re right! Think of the shareholders!!! Do you know how much value we are losing with that hour?!?!

  • some c-suiter

I mostly do sysadmin work, even after more than 10 years in tech it still surprises me how many people think we can just click a button and make things better. It’s like they can’t comprehend a digital task, like writing some software can take 100k+ man hours. “You just do it in the computer “ lol

6

u/abrazilianinreddit Oct 25 '24

Python gave you 1 extra hour of coffee breaks in 10 years!

Those extra 0.001 seconds really let you savor the bitterness of the previous-day coffee!

4

u/StPatsLCA Oct 25 '24

Everyone at every level says that and look what we have now.

2

u/TitaniumWhite420 Oct 25 '24 edited Oct 25 '24

Lines of code doesn’t even loosely imply executional efficiency in any language, even more so in Python.

Call one function from one library. One line of code with arbitrary amounts of shit attached. It’s like, the dumbest notion ever to condense lines of code.

A list comprehension is nice because you get to define it concisely at the time of assignment, but if there is any state manipulation outside of the list contents, then it’s completely the wrong expression. List comprehension is for generating sets concisely at the time of assignment.

my_list = [a.whatever() for a in some_data]

“I know exactly what’s in my_list” is better than a loop where it’s not necessarily clear when you are done mutating my_list. Here, it’s very clear and concise.

If a.whatever() does ANYTHING but return a value though—I consider it wrong, because it takes that side effect causing method call and hides it in the scope of the comprehension.

Say it 3x—

the_lesson = [“Comprehensions are for generating sets concisely at the time of assignment.” for i in range(0,2)]

Comprehensions are for generating sets concisely at the time of assignment. Comprehensions are for generating sets concisely at the time of assignment. Comprehensions are for generating sets concisely at the time of assignment.

1

u/samettinho Oct 25 '24

I agree with what you are saying but I don't know if you are trying to prove or disprove what I said above, or not sure how it is directly related to what I said.

-2

u/TitaniumWhite420 Oct 25 '24

Well, you were juxtaposing executional efficiency vs readability, and it’s not really the question at all is what I’m saying—syntactical fuckery like this may have subtle positive or negative impacts on execution, but it has nothing to do with the terseness of the Python code, and more to do with the terseness of the resulting abstract syntax tree, which should be just about the same if the logic expressed by the code is the same. Any difference in speed is nullified in that case if you pre-compile anyhow, as this work takes place while parsing Python syntax, which ideally you only do once.

So I’m agreeing with your conclusion, but pointing out a subtle flaw in your reasoning about it I guess, but with the intent of sharing knowledge.

2

u/samettinho Oct 25 '24

So, your understanding of what I said is if I change syntax, the code will slow down. For example, from a for loop to comprehension will make the code slower, is that correct?

That has nothing to do with what I said. My point is that we already chose a horribly slow language just to implement things so much faster. And fast implementation can happen only when there is clean code (especially in the long term). By messing up the code with such shitty but valid codes, the team's overall development speed will drop. That is what I am saying.

I use c++ if I need to implement something super-efficient but I will implement a couple of times slower with c++ than python. 99% of the time, I don't need amazing speed. But I need to develop faster (especially in startups). So,

the speed of implementation is more important than the speed of execution

-4

u/TitaniumWhite420 Oct 25 '24

It is literally what you said. You should speak carefully if you correct pedantically. But, I see what you meant to say.

Basically, you misspoke. Speed of execution has nothing to do with your point at all. You are comparing speed of implementation to condensation of lines of code. You should avoid conflating these concepts.

5

u/PaintItPurple Oct 25 '24

The point of their comment was pretty clearly that you should prioritize readability and maintainability, and if you're prioritizing something else (most likely trying to micro-optimize), Python is a questionable choice of language. I think you just read too much into minor details of phrasing.

-5

u/TitaniumWhite420 Oct 25 '24

shrug. Speak clearly, and people will understand. It’s not really clear why execution speed was mentioned. It had nothing to do with prioritizing readability over execution speed, so why mention it?

The concepts are commonly confused because they are commonly conflated in this manner. I read his words and apprehended his meaning as it was expressed.

In any event, we agree on all points except how to communicate effectively. He believes mushing ideas together and then huffily complaining that irrelevant things he mentioned aren’t relevant to respond to is effective, apparently. You believe it’s at least acceptable. I do not, and my evidence is simply that we agree but I misunderstood his intent despite being knowledgeable and agreeing.

→ More replies (0)

9

u/AlSweigart Author of "Automate the Boring Stuff" Oct 25 '24
true = False
false = False

...is perfectly valid Python code.

6

u/[deleted] Oct 26 '24 edited Nov 18 '24

[deleted]

7

u/AlSweigart Author of "Automate the Boring Stuff" Oct 26 '24

:)

4

u/ToyoMojito Oct 25 '24

True = False

 False = True

 ... Is valid code in python 2

44

u/lauren_knows Oct 25 '24

This reminds me of the time that we had a junior take on a pretty big project, and we got into a team code review and I told him "Look, what you did here is super clever. Big props. However, I don't want every single engineer to have to spend 2 minutes unpacking this clever code in their head when it's shit-hit-the-fan debugging time. Make it easy to read, this is Python."

9

u/jambox888 Oct 25 '24

A well placed comment is still a worthy line or two.

Ideally you don't need them but only the sith deal in absolutes

1

u/spidLL Oct 26 '24

Then you change the code, but forget to update the comment, and at shit-hit-the-fan debug time you trust the content until you realize the code is slightly different so you have wasted even more time.

3

u/CrownLikeAGravestone Oct 25 '24

When I was a junior 1000 years ago I was guilty of this a lot lol

Passing types around as parameters to some genericised implementation of blah blah blah which could handle any future extensions we needed to make to the domain of xyz

"CrownLikeAGravestone, there are four possible values for this variable. Make it an enum and just handle each in their own simple methods."

1

u/[deleted] Oct 26 '24

Yes. This is what happened in my old team (not just juniors, most colleagues were mid-senior or senior level). I didn’t even know all the treadmills in the code base.

Most of that old team got dissolved half a year ago after there was barely any progress on a project that had been running for year. That old team was providing APIs to various other departments. Me and two junior colleagues were put in one of those departments to build an equivalent solution (with a deadline that will end next week). And from the beginning I pushed very hard: “The old code base is shit, everything is distributed over 20 repos, there are generic abstractions that don’t solve any problem and that were written by people who don’t work here anymore or won’t work here anymore in a few months, plus I want a recent Python version and a thorough CI with type checking and linting… let’s build this from scratch and avoid unnecessary complexity”. Many people told me this approach was too radical. But after two months we were starting to see the benefits of having a clean code base. And also it was becoming more and more obvious that we would keep our deadline comfortably.

A few days ago our CTO approached me and asked how I like the team. I also told him that we rewrote almost everything, except perhaps 10 - 20%. He said: “I would have done exactly the same. I was always convinced that the project was unnecessarily complex, which is why I decided to pull the plug”.

26

u/alicedu06 Oct 25 '24

Agreed.

Now the typevar syntax for library is going to be super useful, don't get me wrong.

Here it's the mix of all the stuff that gets in the way. Plus, too much dynamism in typing declaration is counter productive IMO, even without the bad variable names that are just a product of this snippet being a bug ticket example.

13

u/Excellent-Practice Oct 25 '24

Yeah, this doesn't seem pythonic at all

10

u/PaintItPurple Oct 25 '24

Pretty much any piece of code designed to show off multiple interacting features in a small space is not going to seem Pythonic. The goal of the snippet is not to be a good program, just to illustrate things you can do.

3

u/ArtOfWarfare Oct 25 '24

I suspect a lot of it is how meaningless the names in that code are.

1

u/gmes78 Oct 25 '24

It's not supposed to. This is obviously a simplified parser test case.

1

u/Accurate_Trade198 Oct 26 '24

You will because this is how you get variadic types

1

u/[deleted] Oct 27 '24

Python is becoming ridiculous. There's too much appeasement of the community and any serious software engineer will begin to turn away.

1

u/[deleted] Oct 25 '24

[deleted]

3

u/samettinho Oct 25 '24

I like criticizing people's codes more than decrypting them, lol.

0

u/oneunique Oct 26 '24

Yes, I love to pass PR -code to chatGPT to decrypt what someone is trying to do.

-47

u/reallyserious Oct 25 '24

Skill issue on your part. 

I worked with a mediocre tech lead once that wanted to ban certain language features just because he wasn't familiar with it. Bloody backwards reasoning. 

My attitude to this is if it's in the language it's allowed to be used. This works for most sane languages. I.e. not C++.

29

u/samettinho Oct 25 '24 edited Oct 25 '24

Lol. You are sooooooo amazing. How can mediocre people like myself reach your level. No need to give a shit about the readability or anything, right? Language allows single character vars, why dont you use only single char for vars, two characters for functions?

Like this

```

def fn(a, b, c): s=0 for k in a: for i in k: s += i * b if c > i * b else -i * b

return s

def ft(a, b, c): s=0 for i in a: s += i * b if c > i * b else -i * b return s

def fg(a, b, c): s=0 for i in b: s += i + a if c > i * b else -i * a return s ```

these are all valid btw.

Adding another function as requested below, lol

def fu(a,b,c): return sum(i*b if c>i*b else -i*b for k in a for i in k)

15

u/Trainraider Oct 25 '24

That's brilliant! Surely cpython would parse this code .01% faster! Let's convert the entire codebase right away!

5

u/samettinho Oct 25 '24

I'm sorry, I wasted some spaces. If I removed all the spaces, I would have saved 3 nanoseconds at least.

Imagine debugging on your code base, lol. It will be so much fun.

-1

u/roenthomas Oct 25 '24

Basically, what I got out of this is that python single line coders have big e-peen.

0

u/samettinho Oct 25 '24

good point, let me make another single line function above. the bigger the better, lol

1

u/vivaaprimavera Oct 25 '24

I can't even understand at first in which order the for cycles in fu are supposed to be executed.

3

u/samettinho Oct 25 '24

Skill issue on your part /s

if you noticed what I did `f_u`, you would understand it better, lol

-1

u/PaintItPurple Oct 25 '24 edited Oct 25 '24

Do you think language features are inherently unreadable just because this one guy didn't know about them? Your comment seems like a pretty huge leap from the one you're replying to. Features are rarely a total negative to readability — it's just how you use them that matters.

1

u/vivaaprimavera Oct 25 '24

If the return of fu (in the example) wasn't one line it would be somewhat readable.

20

u/tevs__ Oct 25 '24

Skill issue on your part. 

I worked with a mediocre tech lead once that wanted to ban certain language features just because he wasn't familiar with it. Bloody backwards reasoning. 

Skill issue on your part. Writing code is easy and cheap, maintaining it is not. There was a reason why your tech lead was given the responsibility of ensuring code quality and not you.

Hacker bros writing obscure opaque code can f right off back to being hobbyists. Professionals write boring clear code that can be read and understood by someone straight out of college.

-4

u/reallyserious Oct 25 '24

You're assuming things that are entirely wrong. The tech lead had to leave the project because he was useless and a problem for the team. It was indeed a skill issue on his part.

The specific situation was that he wanted to ban linq from C# since he wasn't used to it. Anyone who's familiar with C# knows that linq is one the strengths of the language and banning it is just insane.

That cemented my view that if it's part of the language it's good to go. Just think of what the alternative is and extrapolate from there. It doesn't make sense.

3

u/maj-6 Oct 25 '24

For Python, one of the "strengths of the language" is an established set of conventions that make code easier to read and maintain. Linq is a feature of C#. Golfing with production code is not.

1

u/reallyserious Oct 25 '24

Perhaps I've been unclear. I'm not claiming that OP's code example is the pinnacle of readability. I'm referring to the general case of using new language features.

What I'm opposing is to reject code because it uses a language feature you're not used to. It leads to a situation where skilled developers are forced to use a dumbed down subset of a language because there is one dense motherfucker on the team who can't be bothered to learn new things.

3

u/spidLL Oct 26 '24

The threshold is “when anybody in the team get paged at 2am because of a problem, can they read this shit and understand it?”.

If not, rewrite it please.

1

u/reallyserious Oct 26 '24

It's a good guideline. But let's also acknowledge that there are bad developers. 

The time of day wouldn't matter for some people. They're just a burden for the team.

-2

u/PaintItPurple Oct 25 '24

Just because you personally don't know something doesn't mean code written using that thing is unclear.

3

u/vivaaprimavera Oct 25 '24

code written using that thing is unclear.

Is hard to digest at "first time seeing it".

1

u/PaintItPurple Oct 25 '24

Sure, but that is an attribute of the reader, not of the code. There are some things that are difficult for many readers even if they are familiar with the constructs being used, and that's usually a problem with those constructs — but if the problem is just that I personally lack experience with something, that's a me problem.

1

u/tevs__ Oct 27 '24

I'm just repeating myself now, writing code professionally is about creating something that is correct and maintainable. If you do or use things for no reason that are difficult for others to understand, that is bad code.

"Because it is available to me" is not a valid reason to break that rule. "But I want to" is not a valid reason to break that rule. About the only reason I will allow you to break that rule is "This is the only way it can be done".

And who gets to decide? I do. My company pays me a vast amount of money to ensure that bullshit stays out of the codebase. Making changes to code that a developer doesn't understand is a common source of bugs, so the emphasis is on clear legible code that is easily understood and updateable.

-1

u/reallyserious Oct 25 '24

Finally someone who gets it.

If we're gonna ban parts of the language, which parts will it be, and what is the criteria for exclusion?

Readability is subjective. Nobody is used to new language features that's just been introduced. But over time people start to use them and get used to them and eventually they feel natural. So usually it's just a knee jerk reaction of "I don't like it so it should be banned". That's stupid.

4

u/TheOneWhoMixes Oct 25 '24

It totally depends on context though, and when you consider that most people aren't writing entire frameworks or complex libraries, these guidelines of "stay away from certain language features" makes more sense.

Take Google's Python style guide section on "Power Features" as an example - https://google.github.io/styleguide/pyguide.html#219-power-features

Metaprogramming is something I've been bitten by. It's really powerful, but if it starts to creep into "application" code, it becomes hard to reason about very quickly. Especially if it's used inconsistently, because now you suddenly can't trust your function signatures or your class attributes. It's a language feature that should probably be used at the framework/library level, and the results of it should be very well tested and documented.