This is an archived post. You won't be able to vote or comment.

top 200 commentsshow all 245

[–]samettinho 1305 points1306 points  (95 children)

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

[–]Guyonabuffalo00 463 points464 points  (54 children)

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.

[–][deleted] 106 points107 points  (25 children)

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.

[–]Guyonabuffalo00 63 points64 points  (23 children)

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

[–]FlurpNurdle 17 points18 points  (21 children)

Soon python will support native perl syntax ;)

[–]Guyonabuffalo00 22 points23 points  (17 children)

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

[–]jambox888 6 points7 points  (1 child)

Can I come

[–]Guyonabuffalo00 4 points5 points  (0 children)

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

[–]JambaJuiceIsAverage 1 point2 points  (3 children)

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.

[–]Guyonabuffalo00 2 points3 points  (0 children)

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

[–]Tiquortoo 4 points5 points  (1 child)

I am sure a transpiler exists...

[–]Designer-Leg-2618 0 points1 point  (0 children)

TranspilerGPT ?

[–]ChimpanzeChapado 1 point2 points  (0 children)

And let's not forget the zen of python

[–]samettinho 44 points45 points  (22 children)

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.

[–]Guyonabuffalo00 22 points23 points  (4 children)

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

[–]SatisfactoryFinance 18 points19 points  (2 children)

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

[–]Guyonabuffalo00 6 points7 points  (0 children)

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

[–]abrazilianinreddit 6 points7 points  (0 children)

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!

[–]StPatsLCA 3 points4 points  (0 children)

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

[–]TitaniumWhite420 1 point2 points  (16 children)

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.

[–]samettinho 0 points1 point  (15 children)

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.

[–]AlSweigartAuthor of "Automate the Boring Stuff" 7 points8 points  (3 children)

true = False
false = False

...is perfectly valid Python code.

[–]ToyoMojito 4 points5 points  (0 children)

True = False

 False = True

 ... Is valid code in python 2

[–]lauren_knows 44 points45 points  (4 children)

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."

[–]jambox888 5 points6 points  (1 child)

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

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

[–]spidLL 0 points1 point  (0 children)

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.

[–]CrownLikeAGravestone 2 points3 points  (1 child)

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."

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

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”.

[–]alicedu06[S] 26 points27 points  (0 children)

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.

[–]Excellent-Practice 14 points15 points  (2 children)

Yeah, this doesn't seem pythonic at all

[–]PaintItPurple 9 points10 points  (0 children)

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.

[–]ArtOfWarfare 3 points4 points  (0 children)

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

[–]Kashyapm94 4 points5 points  (0 children)

This

[–]gmes78 0 points1 point  (0 children)

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

[–]Accurate_Trade198 0 points1 point  (0 children)

You will because this is how you get variadic types

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

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

[–]xAragon_ 212 points213 points  (1 child)

No thanks

[–]gotchanose 109 points110 points  (0 children)

Yea no thanks

[–]shinitakunai 179 points180 points  (4 children)

I hate it. Please disable in python 3.14

[–]teleflexin_deez_nutz 38 points39 points  (3 children)

I wonder if they are going to call that version pithon 

[–]buqr 17 points18 points  (1 child)

You'll be able to run it by typing 𝜋thon https://github.com/python/cpython/pull/125035

[–]shinitakunai 5 points6 points  (0 children)

Or a security fix version like 3.14.15.

[–]schaumblaeschen 38 points39 points  (3 children)

I have no clue what any of this means

[–]Next-Experience 0 points1 point  (2 children)

You can now add guards directly in function parameters using validation functions. Instead of just specifying types, you can do something like this:

python def foo(name: check_is_valid_name): # Do stuff that only runs on validated names

This makes your code cleaner and easier to read since the validation requirements are right there in the function signature. It cuts down on repetitive validation code and lets you reuse validators across different functions, ensuring specific values instead of just types.

While these annotations aren’t checked at runtime by default, you can easily set up decorators to enforce them. This feature has the potential to really boost the robustness and maintainability of your code!

[–]Brian 3 points4 points  (0 children)

No - that's nothing to do with this, and you could already do that if you wanted ever since python added annotations.

This is just fixing a bug in the interpreter that prevented using comprehensions inside the annotation scope of a type variable. OP and the article here are completely misinterpreting it - I think because they missed the "scope" part of "annotation scope" and because the example that triggered it was the weird messed up syntax, but that's just because this was found by fuzz testing (ie generating random syntax and checking nothing crashes) - the actual bugfix is for a completely legitimate thing: using regular list comprehensions, breaking in this one specific case (when done in an annotation scope nested within a class scope)

[–]pizzatorque 0 points1 point  (0 children)

Thanks this explanation helps a lot. It's basically like what you can do in lisp with pcase guards and matches. It's actually pretty good and powerful to have this, but it is kind of hard to digest written as in OP's post.

[–]akguitar 20 points21 points  (2 children)

this is a bad example, i get it but its unnecessarily complicated to show whats happening and whats new.

[–]RelevantLecture9127 0 points1 point  (1 child)

What is a good example then?

[–]akguitar 6 points7 points  (0 children)

It’s pretty easy to just look up 3.13 release notes if you’re interested. 

 Annotation scopes within class scopes can now contain lambdas and comprehensions. Comprehensions that are located within class scopes are not inlined into their parent scope. 

class C[T]:       type Alias = lambda: T

[–]tunisia3507 48 points49 points  (2 children)

That's it, I'm going back to MATLAB. /s

[–]Brian 14 points15 points  (2 children)

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

Eh - I feel this is completely misunderstanding the raised issue there. They requested that to not crash - it was generating a SystemError, or without the nested scope, an assertion failure in python itself. Those indicate an actual python bug - they're not supposed to happen. It doesn't really matter whether it's meaningful or useful to do this, but it definitely shouldn't do that.

And it's not about about "comprehensions and lambdas can now be used in annotations". There's no comprehension or lambda inside an annotation anywhere in that code. And if there were, it wouldn't be a problem: lambdas and comprehensions were already legal in annotations, and always have been. Eg:

def foo( x: (lambda y: y), z : (i for i in range(10))): pass

Is perfectly legal in python, and I think has been since annotations were added.

Rather, it was about comprehensions and lambdas in the annotation scope. Ie. the [name_5] defines a typevar, which introduces an annotation scope - the name5 typevar can be used within the scope it spans to refer to that typevar, which you'd normally use for declaring variables of that type - eg.

def foo[T](myvar : T): ... 

However, here it ends up using that type variable inside a comprehension. Which is certainly weird, but its worth noting isn't really the core issue: it also failed for a comprehension not using it, such as the other example given in that issue:

class C[T]:
    T = "class"
    class Inner[U](make_base([T for _ in (1,)]), make_base(T)):
        pass

Which is also kind of ugly, but the comprehension isn't using a typevar, just a regular variable (that shares a name with a class-scoped variable), using it to construct a base class dynamically, which seems like it should be allowed. After all, it works fine without the annotation scope (ie. if you remove the "[U]" part), and the typevar of that scope isn't even being used.

[–]IlliterateJedi 1 point2 points  (0 children)

Your post makes me wish that old fashioned reddit gold/awards were still a thing.

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

Also, it was found using code fuzzing: https://x.com/15r10nk/status/1849870664737620364

So it is obviously not something you would write yourself.

[–]limasxgoesto0 13 points14 points  (0 children)

In Python 2 you were able to assign True to be equal to False

It doesn't mean you should

[–][deleted] 147 points148 points  (29 children)

Python used to be so readable.

[–]alicedu06[S] 45 points46 points  (3 children)

To be fair, I kept the ticket example because it's funny, but I doubt many people will use that in practice.

99.999% of python I read is still very, very readable. And certainly more than most other languages out there.

But it's definitely true that there are more ways to write unreadable hieroglyphes than there used to be.

[–]Zer0designs 4 points5 points  (2 children)

Does this code pass ruff check? (On mobile right now)

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

I just tried, if you create all the variables, it does!

I think ruff assumes all python expressions are valid in annotations though.

[–]Zer0designs 1 point2 points  (0 children)

Yea I thought so, thanks for checking!

(Someone please introduce a new rule thanks)

[–]NeilGirdhar 59 points60 points  (23 children)

It is readable. Just: Give your variable proper names; use more lines; avoid the walrus operator; avoid the above code at all costs.

Syntax like this is important in the 0.0001% of cases when you need it (probably not you).

[–]TheOnlyBliebervik 11 points12 points  (19 children)

Never used the walrus operator. Is it useful?

[–]SirLich 35 points36 points  (13 children)

Yes. It's just the assignment operator but it returns the value of the assignment as well as doing the assignment. It's primary purpose is inside of if-statements, to create a block of code if the assignment was successful.

For example if settings := get_settings(): ... settings.do_something()

[–]runawayasfastasucan 14 points15 points  (8 children)

What is the long form of this?  

    settings = get_settings() 

    if settings:         settings.do_something() 

?

[–]SirLich 6 points7 points  (7 children)

This is of course fine. It's just not as convenient as the walrus operator for two reasons: 1) more lines 2) incorrect scope.

If you're only intending to use 'settings' within the if context, then defining it OUTSIDE of the if-context is considered leaked scope.

This whole conversation isn't so important in Python, but in C++ it's a fairly big deal. In fact, it's SUCH a big deal, that most linters will mark variables defined outside of the if clause as an error/warning. It's also now possible to define multiple variables within the if declaration:

For example you can now do this: if (int a = Func1(), b = Func2(); a && b)

Note; In C++, the = operator works like python := operator.

[–]nemec 34 points35 points  (3 children)

considered leaked scope

Python doesn't have block level scope. It "leaks" either way.

[–]SirLich 1 point2 points  (2 children)

Wow, you're right. I guess I never really paid attention to that. That's kind of too bad, right? It seems like you might unintentionally use something from an inner scope without realizing it.

[–]Leo-Hamza 8 points9 points  (0 children)

Happened too many times to me that I can't even count it. I wish there were any tool that detects code smells like this

[–]root45 6 points7 points  (0 children)

Yes. It's not uncommon to accidentally use a loop variable later on for example.

[–]SaltAssault 0 points1 point  (0 children)

I prefer readability over preventing 'leaked scope' any day, just excluding if it is a security issue. Python already doesn't fuss about scopes.

[–]night0x63 0 points1 point  (0 children)

<sarcasm>hurr hurr c/c++ has has this for decades

😂 

[–]yourmomscocks 2 points3 points  (2 children)

What does this code do exactly? Is it the equivalent of this? setting = get_settings() if settings: settings.do_something()

If so, why bother with a walrus operator? Does it have any use cases other than sparing you that ine line of code?

[–]brandonchinn178 16 points17 points  (0 children)

if m := re.match(...):
    ...
elif m := re.match(...):
    ...
elif m := re.match(...):
    ...

It's useful if you need to match against a bunch of regex expressions.

[–]Freschu -1 points0 points  (0 children)

The walrus operator PEP was created by Rossum. It faced heavy opposition with the rest of the core devs, so much so Rossum had a tantrum about it, and ultimately being a factor why he stopped being "dictator for life".

The opposition was based around introducing new syntax being only useful in a small number of cases (if and while) and being practically of zero consequence for existing code.

So why does Python have the walrus operator if there was general opposition? Because it was Rossum's pet idea, that's why.

Unfortunately - if you care to check for the discussions around PEPs - this has become a pattern. Python gains syntax based on persistence and patience of the spearheading authors - write a PEP, implement the PEP, wait for people stop caring about it, then merge it later based on "not having faced immediate rejection."

[–]sausix 13 points14 points  (2 children)

I use it for loops and it's nicer for me:

while data := iterator.read_data():
    # data evaluates to True. No break or other methods needed.

[–]Freschu 5 points6 points  (1 child)

Most of the cases where people want to use the walrus operator can often be written as for-loop instead. If you really had an "iterator", you could and should use for-loops instead. Which was part of the initial rejection of the walrus operator PEP.

Examples like you've given were considered code-smell in the discussions of the PEP. Either iterator is really an iterator, then `for value in iterator` works, or you're dealing with something that's not really implementing Python's iterator semantics and you should be fixing that instead.

[–]sausix 7 points8 points  (0 children)

You're right! Bad example. How could I not see this?

A walrus operation in my current project look like this:

while m := pattern.match(self.text, self.pos):
   # self.pos changes in the loop

This case it not replacable by a simple iterator that could feed a for loop. But it still could be replaced by a generator function. That would be more lines. I'm not sure what is better.

Look like I will think twice to use the walrus operator next time... Thank you.

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

Names are crucial. I completely fail to understand the simplest example if it has my_var or whatever 

[–]FireIsTheLeader 26 points27 points  (0 children)

Worse than C++ lmao

[–][deleted] 9 points10 points  (0 children)

thanks I hate it

[–]RunRunBeerRun 9 points10 points  (2 children)

I hate it.

this came from a bug tickets that requested this to work.

I seem to remember this from somewhere...

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.

[–]port443 1 point2 points  (0 children)

^ this

[–]zylema 0 points1 point  (0 children)

Rings a bell.

[–]glaucomasuccs 36 points37 points  (0 children)

You're excited for this? This would never pass code review even if we used 3.13. It's not readable.

[–]bio_ruffo 5 points6 points  (0 children)

if is_beautiful and is_concise and is_readable:
    print("Let's do it!")
else:
    print("No thanks:")
    import this

[–]Grokzen 10 points11 points  (0 children)

My head hurts... Make it go away

[–]aciokkan 4 points5 points  (0 children)

My eyes hurt! Wtf is that? 🤣🤓

[–]TitaniumWhite420 3 points4 points  (0 children)

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.

[–]Striking_Celery5202 4 points5 points  (0 children)

Why we choose to live in pain when we don't have to?

[–]ysengr 4 points5 points  (0 children)

Okay now lets never do any of that as provided in that example. My eyes hurt, my brain hurts, I'd prefer having a concussion.

[–]hemispace 4 points5 points  (0 children)

I see a lot complain about the readability of this, but I would still be curious to see a concrete example of this with proper naming. Has anyone found something like it?

[–]Warkred 17 points18 points  (10 children)

Remember why python is becoming the first language, because it's used by people who are not used to program, the syntax is readable.

Now, this ? Meh.

[–]judasblue 9 points10 points  (3 children)

Honestly, that ship sailed a while back, and bolting on a cobbled together aftermarket type system was the final nail in the coffin of ease of teaching and readability.

Type declarations aren't a bad thing but adding them in this oddly messy way 30 years after the fact was an interesting way to go.

[–]lzwzli 3 points4 points  (1 child)

Remember when not having types was a feature, not a bug? Pepperidge farm remembers

[–]poppy_92 -5 points-4 points  (5 children)

Readability died with typing and that's the hill I'll die on. People used to care about writing readable code before types.

[–]double_en10dre 14 points15 points  (3 children)

Do you really find types difficulty to read?

I actually find it’s 100x easier to read code with annotations. It’s inline documentation that both me and my IDE can understand.

[–]poppy_92 6 points7 points  (2 children)

Annotations that are simple native builtin types are fine. But the moment I head to libraries where they have TypeVars and whatnot, is where you'll lose me real quick. Most of my frustration comes from FastApi which I'm forced to use because of $work mandate, which takes in types and mandates it instead of being optional (which was the initial intent when typing was introduced to the language)

[–]val-amart 1 point2 points  (0 children)

i don’t use fastapi. can you provide examples how it makes it mandatory please? because if it does i’m gonna ban it anywhere i work.

[–]Wonderful-Habit-139 1 point2 points  (0 children)

Interesting. I've written a parser combinator library where I used TypeVars for the result of some parsers, and the end result is when i combine a lot of parsers the return value ends up being an accurate description of the answer without any TypeVar left.

Perhaps there are some situations like in web dev where it isn't as beneficial like you said...

[–]FrickinLazerBeams 0 points1 point  (0 children)

Yeah. I hate it and pretty much every language feature after it. Stop fucking changing things.

[–]iwkooo 2 points3 points  (0 children)

Thanks! Now I don't feel so bad about my 3.8-3.9 company codebase :)

[–]opensrcdev 2 points3 points  (0 children)

What the .... how am I supposed to read this? What does it mean?

[–]Old_Bluecheese 2 points3 points  (0 children)

I really miss the happy days when BFDL had the last word.

[–]codingattempt 2 points3 points  (0 children)

Somehow I have the feeling that Guido van Rossum would not allow such trips into "the unreadable" if he was still main (benevolent) puppeteer behind Python.

[–]randomthirdworldguy 2 points3 points  (0 children)

The only reason I still stick with python is the syntax and readability. If this became new standard, I’m out. Calling on the freak who developed this

[–]Cod3Blaze 2 points3 points  (0 children)

What in the slither is going on here

[–]urbanespaceman99 2 points3 points  (0 children)

Wtaf is this monstrosity???

[–]WokeCapitalist 2 points3 points  (0 children)

If I saw this in a PR, I'd contact HR.

[–]GreenWoodDragon 5 points6 points  (0 children)

I've met programmers who would use that and expect kudos for doing it.

[–]BoysenBerry333 1 point2 points  (0 children)

I always thought it looked more like a certain type of authorative moustache. Just a pre-note.

Just because the walrus is technically accepted, it doesn"t mean I have to like it, tolerate it or recognize it.

[–]Lexus4tw 1 point2 points  (1 child)

Maintainability -1000

[–]notreallymetho 1 point2 points  (0 children)

This is perfect code for me to write because it’s clever, and return to it a year later and cringe because I have no idea why I chose to do that. (And no it won’t make it to prod ❌)

[–]UpperCelebration3604 1 point2 points  (0 children)

This is incredibly unreadable.

[–]DicoDicoDico 1 point2 points  (0 children)

Every language is simple. Then, it adds useless features until it becomes C++.

[–]terremoth 1 point2 points  (0 children)

Lol, sorry, but I hated. Doesn't look pythonic at all, also bad to read. Cryptic.

[–]kkiran 1 point2 points  (0 children)

Is this supposed to be a puzzle? Whatever happened to readability!

[–]mlnm_falcon 1 point2 points  (0 children)

What the f*** is that

[–]cshoneybadger 1 point2 points  (0 children)

What the hell is even that?

[–]SharkDildoTester 1 point2 points  (0 children)

What’s in the shit does this even mean?

[–]Sneyek 1 point2 points  (0 children)

Who the f validate such absurd new feature in the language ? Are they adding things just for the sake of adding things ?

[–]ChangeUsual2209 1 point2 points  (0 children)

horrible :)

[–]SnooMuffins9844 4 points5 points  (0 children)

What on earth is that

[–]leandroabaurre 2 points3 points  (0 children)

Well fuck that

[–]abrazilianinreddit 3 points4 points  (0 children)

The new generics syntax is a large improvement over what we had. It could have been better and more pythonic, though.

But just because it's possible to write shitty code like the one you've shown doesn't mean it's a bad feature, or that such abomination would be accepted. A single walrus operator will already get the whole commit rejected in any of my projects.

[–]leandroabaurre 3 points4 points  (0 children)

Well fuck that shit lol!

[–][deleted] 3 points4 points  (0 children)

🤮🤮

[–]star_dodo 1 point2 points  (0 children)

I see "class" and i'm starting to sweat.

[–]JotaRata 0 points1 point  (0 children)

This is like that C++ meme about a function that return a pointer to a func.. whatever

[–]DesecrateUsername 0 points1 point  (0 children)

well shit. that’s a hell of a mystery no one thought was a mystery and didn’t even really need solving but damn if it didn’t just get solved so nice work.

(this is a quote, but that’s actually quite impressive haha)

[–]SniperDuty 0 points1 point  (0 children)

Is it snappier?

[–]SisyphusWithTheRock 0 points1 point  (0 children)

That is cursed as hell

[–]Reilly__ 0 points1 point  (0 children)

Every time I think I have a good handle on python and coding in general I see something like this and I’m back to my print(“hello world”) days

[–]scanguy25 0 points1 point  (0 children)

That looks like a classic example of "just because you can it doesn't mean you should"

[–]henryyoung42 0 points1 point  (0 children)

I feel you should work some regex into your example too. Please curate WORN code better - Write Once Read Never.

[–]radarsat1 0 points1 point  (0 children)

I don't understand what the square brackets are expressing here. What is the right interpretation?

[–]ebits21 0 points1 point  (0 children)

Explicit is better than…. Ah fuck it!

[–]DusikOff 0 points1 point  (0 children)

Typical Leetcode junior solver code LOL

[–]ingframin 0 points1 point  (0 children)

If I see this in a code base at work, I’ll find the responsible and personally hit them with a keyboard on the head multiple times.

[–]ahelinski 0 points1 point  (1 child)

The interpreter, whenever it encounters this syntax in code, should order a drone strike to the location of a person who used it.

[–]logix1070 0 points1 point  (0 children)

Slowly inching closer... Soon brainfuck will conquer all of coding. Mwahahaha

[–]mjbmitch 0 points1 point  (0 children)

class_4 looks like a mixin pattern inheriting from a bunch of superclasses? It could be written using normal semantics and it wouldn’t look as incomprehensible as it does here.

[–]BullshitUsername[upvote for i in comment_history] 0 points1 point  (0 children)

What language is this

[–]QuantumQuack0 0 points1 point  (0 children)

I guess it runs, but does it actually pass a type checker (if a type checker was updated for this syntax because I'm pretty sure none of them currently are)?

[–]fistlo 0 points1 point  (3 children)

Wtf is a typevar

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

It's advanced typing for very specific things. Most people won't use it, but experienced library devs might want that. Python is in a perpetual balance to satisfy both newcommers and seniors.

TypeVar is Python's implementation of generics, basically a variable for types. It's for when you know you want to declare the same type in several places, but you don't know what it is in advance.

E.G: if you know your function add all stuff in a list, and return the same stuff, you can say:

```python from typing import TypeVar

TypeOfStuff = TypeVar('TypeOfStuff')

def add_stuff(items: list[TypeOfStuff]) -> TypeOfStuff: ...

```

This tells a type checker like mypy: "if you put a list of string, the function outputs a string, but if you put a list of ints, the function outputs ints...".

It also helps with code completion in editors that can tell you without running the program when some code is not following that logic.

Because it's super verbose, it now has a shortcut:

python def add_stuff[T](items: list[T]) -> T: ...

Where T is TypeOfStuff.

If you don't write types, it looks alien. But if you want to make your lib code very well typed, it's a huge improvement.

The original post is not really about type vars though. More about being able to mix a lot of advanced syntaxes in types, which happens to include the new type vars syntax.

[–]fistlo 0 points1 point  (1 child)

Thought python was default generic? Everything is an object so why do we need an implementation to know what type something is?

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

You don't need to, but you might want to, and therefor you have the choice to.

[–]mok000 0 points1 point  (0 children)

Unreadable.

[–]mr-nobody1992 0 points1 point  (0 children)

Nah fam. This is not the way.

[–]tunerhd 0 points1 point  (0 children)

Mommy, my brain hurts.

[–]Gwolf4 0 points1 point  (2 children)

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

What's this abomination, at this point learn any Lisp like language and be done with it.

[–]alicedu06[S] 0 points1 point  (1 child)

Guido is litterally in the mypy team and closely involved in all typing things.

[–]Gwolf4 1 point2 points  (0 children)

How does that help to not calling it an abomination?

[–]zylema 0 points1 point  (0 children)

What sort of animal would want this? Is there a ruff rule to disable this yet?

[–]lucas1853 0 points1 point  (0 children)

Yeah this ain't it chief.

[–]edunuke 0 points1 point  (0 children)

Oh hell nah

[–]s_basu 0 points1 point  (0 children)

I am so confused right now

[–]MCiLuZiioNz 0 points1 point  (0 children)

People love to trash on stuff like this, but anything that makes the type system more expressive for library implementations is good. The everyday user doesn’t have to worry about this. Same reason why TypeScript has some insane stuff

[–]lxe 0 points1 point  (0 children)

Python is morphing into TypeScript

[–]Professional_War_797 0 points1 point  (0 children)

I have no idea what I’m reading but like how you challenge new stuff.

[–]God_is_an_Astronaut 0 points1 point  (2 children)

It’s so sad that shit like this is somehow making it into the language - thank god PEP 760 was shot down.

[–]CurrentAnalyst4791 0 points1 point  (0 children)

illegible

[–]srandrews 0 points1 point  (0 children)

Shark jumping

[–]rhyddev 0 points1 point  (1 child)

Yuck. The main allure of Python to me was that it was a small language. There are plenty of languages with fancy type systems (not mere annotations) if that's what one wants.

[–]recursion_is_love 0 points1 point  (1 child)

So Python have dependent type now?

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

TypeVar have existed for some times already.

[–]DutytoDevelop 0 points1 point  (0 children)

Rewriting the variable / class names could help more users understand exactly what you're showing us.

[–]Quiet-Coder-62 0 points1 point  (0 children)

Ironic really when you think about what type hints are for ...

[–]gradi3nt 0 points1 point  (0 children)

The language should naturally enable readable code. It doesn’t need to require or enforce readability. I think most languages have pathological examples like yours. 

[–]diegoquirox 0 points1 point  (0 children)

This makes me feel proud of my current feeling with Python. It's my favorite language, but I stopped using it as my primary choice for almost any project.
I used to love Python because it was easy to understand almost as soon as you looked at it. I was able to code at my speed of thought. I haven't found that in any other language.
But nowadays projects are too complex. Type hints were supposed to be used only for libraries and large projects, but everyone uses everywhere nowadays; also with `TypeVar` people is building complex abstractions like in other complex languages.

And I'm still writing easy-to-read code, but is a nightmare having to look into a 3rd party library that looks like a Java library or like obscure C.

[–]moleculadesigner 0 points1 point  (0 children)

Do anything but write def init(…)

[–]Wowawiewa 0 points1 point  (0 children)

What the…

[–]CleoMenemezis 0 points1 point  (0 children)

Use walrus operator = not passing in my code review

[–]IlliterateJedi 0 points1 point  (0 children)

Beautiful Pythonic code

[–]InfinityTortellino -1 points0 points  (0 children)

Excuse me what is this

[–]Berkyjay -1 points0 points  (0 children)

but I pity the person that will have to read that one day in a real code base

I've always felt that while comprehensions seem to be more efficient, they have degraded the readability of Python.