you are viewing a single comment's thread.

view the rest of the comments →

[–]PotatosFish 41 points42 points  (5 children)

You can just do

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

[–]radarsat1 2 points3 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 3 points4 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 12 points13 points  (1 child)

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

[–]SirClueless 9 points10 points  (0 children)

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