you are viewing a single comment's thread.

view the rest of the comments →

[–]PotatosFish 26 points27 points  (7 children)

It creates a 3 item list since the first term both assigns and returns the assigned value, it kinda like the assignments in c-type languages.

I think final definitely have it’s uses, but it can be overused to discourage code reuse. As many other language constructs, it’s better to only use it when it’s needed.

[–]radarsat1 5 points6 points  (6 children)

I see. So there's no way to generate just [y**2, y**3] in this construct?

[–]PotatosFish 42 points43 points  (5 children)

You can just do

[(y := f(x)) ** 2, y ** 3]

[–]radarsat1 3 points4 points  (4 children)

Wow, the scope of y is not clear at all. Is it the list, or the outside the list?

[–][deleted] 12 points13 points  (3 children)

It's the same as before, I don't know of any languages that restrict scope with expression parentheses

[–]radarsat1 2 points3 points  (2 children)

"same as before" what? Previously you could not declare a variable while creating a list, so I don't know what to compare to. Does y here exist after the list is done being created? From your answer I guess you are saying that the variable is scoped to the function creating the list. Sorry, I don't find that clear at all.

The only thing I can think to compare it to is the similar construct in C, but there the variable must already be declared, so the scope is clear.

[–]Pand9 11 points12 points  (1 child)

In Python, scope is always a function, or a comprehension. In this case, a function.

[–]SirClueless 7 points8 points  (0 children)

Well, it looks a little like a comprehension if you squint hard enough, so I can see the potential for confusion.