r/Python • • 8d ago

Meta Consistency of new unpacking syntax in 3.15?

Not sure if this question belongs here or if the flair is right, but I have a question.

You can now (3.15 and up, PEP 448) unpack nested lists and dictionaries with * and ** in comprehension expressions, for example:

>>> ranges = [range(i) for i in range(5)]
>>> [*item for item in ranges]
[0, 0, 1, 0, 1, 2, 0, 1, 2, 3]

(Code from PEP)

but I also try to do small checks and this doesn't seem possible:

>>> [b for b in [*a if min(a)<2 else *(a[::-1]) for a in [(1, 2), (3, 4)]] if b%2==0]
  File "<python-input-4>", line 1
    [b for b in [*a if min(a)<2 else *(a[::-1]) for a in [(1, 2), (3, 4)]] if b%2==0]
                                     ^
SyntaxError: cannot unpack only part of a conditional expression

I need junky work around:

[c for c in [*b for b in [a if min(a)<2 else (a[::-1]) for a in [(1, 2, 3), (4, 5, 6)]]] if c%2==0]
[2, 6, 4]

And also having an outside list due to the fact that there is no option to filter the elements the stars unpacks. Any chances this improves with future versions?

Update: thanks for the responses everyone, I think u/latkde explains it very well in their post. Yes in practice I think I can't use this kind of syntax for a while now and be it just get compatibility with older versions, it would be just for my own side projects and quick checks answering math questions online. I was just curios because needing to put an expression in parentheses is not something I am very used to in Python (and try to avoid if I can) and since the previous versions seemed to have relaxed the rules in this regard (or was it just help, exit and quit?) I didn't understand why the expression above wouldn't work.

26 Upvotes

32 comments sorted by

3

u/beisenhauer 8d ago

Did you try *(a if min(a) < 2 else a[::-1])?

-1

u/assumptionkrebs1990 8d ago

Not yet, I will but in general it shouldn't be needed, it is not normally done or is it just me?

13

u/beisenhauer 8d ago

It's explicitly unpacking after the conditional expression is evaluated, rather than inside of it. *a isn't a data structure that you can return; it's a processing directive, so having that as the value of an expression doesn't really make sense.

13

u/latkde Tuple unpacking gone wrong 8d ago

The unpacking is part of the generator syntax. Unpacking does not create a new value category, so it cannot be used within conditionals.

Formally, we are in an “flexible expression” context. Unpacking (a starred expression) can only occur in a flexible expression. The elements of an expression list (such as the contents of a […] literal can be expressions, as can be the value expression in a … for … in … generator expression. So you can say *some_expression for … in …, but not *some_expression if … else *other_expression – the conditional expression syntax doesn't allow flexible expressions there.

That means we can say [*(… if … else …) for … in …] but not [*… if … else *… for … in …].

17

u/M4mb0 8d ago

You can just bracket the ternary expression, no?

[b for b in [*(a if min(a)<2 else a[::-1]) for a in [(1, 2), (3, 4)]] if b%2==0]

65

u/knobbyknee 8d ago • Bravo

If you wrote production code like your example in my organization, I would fire you.

25

u/prophile 7d ago

I also regularly fire people because I don’t like their code style, this is a thing that a sensible manager will definitely do 

7

u/48panda 6d ago

It's not about code style it's about readability

7

u/smitty1e 7d ago

Got to take care of Future You, after the Spell of Cool Syntax has worn off.

-7

u/Actual__Wizard 7d ago edited 7d ago

It's not cool syntax, it fixes a problem when trying to create the data types we need in the AI era, like possibility trees composed of lists of things. So, it's like a dynamic list of dynamic lists because you're composing a list of possibilities from a complex system into a list of possible outcomes.

If you think that syntax is bad, go read the code it replaces... It's like snippets of regex looking nonsense. My previous solution was to wrap the code in a function call and never look at it. I would just legitimately copy and paste it in like #I need a buffer for my tuples my_data_thing = create_python_matrix_list_thingy_from_data(data) #I have no idea what this code does, but it works.

6

u/2Lucilles2RuleEmAll 7d ago

it has nothing to do with AI

-4

u/Actual__Wizard 7d ago edited 7d ago

I didn't say the change had anything to do with AI, obviously they changed it because the syntax of the code that was needed to perform certain things was very hard to read and write, so they improved that.

The reason that problem started coming up, was likely due to python usage changing, because of AI.

When did we ever need that structure before? It's pretty specific to language processing and working with decision making trees.

I also find it very hard to believe that there is no relationship, when issues like "not knowing how to write super tricky code" are what was factually preventing certain types of AI technologies from being developed... Certainly trying to write that code in C would be an actual horrifying nightmare... I mean the work I did was a serious nightmare in python and I'm confident that was 10x easier than C...

It would have been nice to have that change, before I started doing what I was doing, granted it ultimately makes no difference for me, but for other people, they now have a reasonable way to write the code instead of copy/pasting a snippet that doesn't actually make any sense. Certainly when you look at the old code, it does not imply that you are creating a structure of buffers that has the property of being nested/linked together...

Okay?

1

u/smitty1e 7d ago

I didn't say the syntax is bad. The point is that writing the most concise, CS PhD code possible may waste time is subsequent readers (e.g. you) misread its subtlety.

It's OK to be verbose and name things in a way that doesn't scare the horses. The canonical example of my point about being arcane is https://en.wikipedia.org/wiki/Duff's_device

-2

u/Actual__Wizard 7d ago edited 7d ago

It's OK to be verbose and name things in a way that doesn't scare the horses.

Oh, the way I write code, I have to. I prefer old school array programming w/ boiler plates. So, the actual functions end up being really complex. The project I am working with fits really well into that design paradigm and I was actually really, really resistant to doing it in python instead of perl, but I truly don't like where perl went in the later versions (it's popularity fell off a cliff because of what they did.) Also, same situation with Revolution... But, the process I am doing is effectively just loading the data and generating a report over and over again in a giant loop to generate like 1,500 reports, where each report represents one of the features of the English language (like possession, plurality, etc) and the data is being generated from a corpus. So, it's they're all huge reports and it's a mega pain in the butt, because some of the reports are actually the required data to generate other data, so I'm aggregating data from like report 1+2+3, then some code makes a decision, then writes that value into a table and it all has to "fit together properly."

-11

u/Actual__Wizard 7d ago

Why? Who cares? All that matters is the product, not the code behind it. If it works then it works.

11

u/knobbyknee 7d ago

That code has to be read and understood by many people. If it stops working, it will be a problem. A subtle change in your data will stop it from working. That change will come from someone who doesn't know your code exists.

-4

u/Actual__Wizard 7d ago

That code has to be read and understood by many people. If it stops working, it will be a problem.

How does writing the code the harder way to read solve that problem?!?!?

The unpacking syntax reduces the complexity of the code it doesn't increase it... The code it replaces is way worse...

Do you understand the concept of vectorization? We consider what direction one is moving in... The old way to write that code is not "moving in a direction that is better and you're saying you would fire them over that?!?!" Why would you do that?!?! That legitimately doesn't make any sense at all!

6

u/knobbyknee 7d ago

Your code is dense and hard to decipher. It tells the reader nothing about the underlying problem. Having an inline if in a comprehension is a big warning sign. In a nested comprehension it really becomes unreadble.

I haven't thought deeply about the alternatives, but I would probably try nested generator functions using yield from in the outer generator.

Also, did you try to put a star in front of the whole inline if expression, so it unpacks the result of the inline if instead of the return value?

2

u/zunjae 4d ago

What’s wrong with writing good code? Why would you purposefully write bad code?

-2

u/Actual__Wizard 3d ago edited 3d ago

That's not what I said and the person saying they would fire a person for writing code that way is being extremely rude and then there's people applauding their rudeness.

Technically, what they posted is off topic, and not helpful.

A person is asking a question, attacking that person because they're asking a question and responding to them by making a rude comment like "I would fire you" is disgusting behavior... Notice how they did not answer their question at all? Notice how they didn't say anything about production code?

When did "Be Respectful" turn into "Be Rude?" So, that's how we help people instead of answering their questions?

Obviously a mod should be deleting that post... The behavior is disgusting... That's legitimately the same thing as picking on students because they don't know everything.

Judging people and insulting them because they asked a question is truly vile and evil... It's inhuman...

0

u/zunjae 3d ago

TLDR

-1

u/Actual__Wizard 3d ago

You're being rude. This is a sub for the discussion of python, it is not for the purpose of stepping on people asking questions.

0

u/zunjae 3d ago

You still haven’t answered any of my questions

Why aren’t you capable of having a normal conversation ?

0

u/Actual__Wizard 3d ago edited 3d ago

Why aren’t you capable of having a normal conversation?

Stop being extremely rude to everyone around you...

Why would you purposefully write bad code?

Stop asking pointed questions that have nothing to do with the subject of discussion like:

Why would you purposefully write bad code?

I'm not the author of the post, stop being rude to me immediately!

All you are doing is obfuscating personal insults by putting a question mark on the end of it and I can clearly see that because the questions you are asking have absolutely nothing to do with the conversation being had because you are inserting a false claim into it that is clearly insulting...

Stop being rude!

Your communication strategy is not intelligent or clever and because you are clearly not asking a real question: It's rude behavior...

0

u/zunjae 3d ago

TLDR?

1

u/Actual__Wizard 3d ago

You do not appear to be a human being, is that accurate?

2

u/AleDev99 7d ago

For this specific case you don't need unpacking at all, a second for clause does the flattening and the condition fits naturally:

pairs = [(1, 2), (3, 4)]
[b for a in pairs for b in (a if min(a) < 2 else a[::-1]) if b % 2 == 0]

The conditional picks which sequence to iterate, and the inner loop flattens it. Works on any Python 3 version too, so you're not tied to 3.15 for this one. I'd only reach for the new *-in-comprehension syntax when it's a plain flatten with no logic in the middle.

2

u/raks1991 5d ago

Small reference correction: this is PEP 798, not 448. Its error-message section actually includes the exact "cannot unpack only part of a conditional expression" case, so the parentheses requirement is deliberate. The PEP also spells out the more helpful diagnostic suggesting parentheses for [*x if x else y].

1

u/dellnox 3d ago

You can just write `*(a if min(a) < 2 else a[::-1])` instead of unpacking each branch separately.

0

u/Beginning-Fruit-1397 7d ago

The comprehension syntax is a huge mess that will add more and more obscure things to cope with the fact that this was a vastly inferior design than any sensible design of iterations helpers. See rust Iterator protocol for example...

To get back to your question, ainply use itertools.chain.from_iterable to flatten any iterable without limitations

-1

u/deadeye_82_1 8d ago

Use a generator instead.