[Python-Dev] PEP 572: Assignment Expressions (original) (raw)
Chris Angelico rosuav at gmail.com
Sat Apr 21 03:46:44 EDT 2018
- Previous message (by thread): [Python-Dev] PEP 572: Assignment Expressions
- Next message (by thread): [Python-Dev] PEP 572: Assignment Expressions
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Sat, Apr 21, 2018 at 5:11 PM, Steven D'Aprano <steve at pearwood.info> wrote:
On Sat, Apr 21, 2018 at 12:30:36PM +1000, Chris Angelico wrote:
Introducing expression assignments will make these oddities even more obvious. You'd be able to demonstrate things like this at function scope, not just with a class. In what way? And are you absolutely sure they will be oddities? To give an analogy, I don't think this is an oddity: py> def func(a=1, b=a+1): ... pass ... Traceback (most recent call last): File "", line 1, in NameError: name 'a' is not defined If anyone expected that the default value for b could make use of the default value for a, the answer is: no, Python function declarations don't work that way. Maybe they could, if we wanted them to, but we don't, so they don't. So can you explain specifically what odd function-scope behaviour you are referring to? Give an example please?
doubled_items = [x for x in (items := get_items()) if x * 2 in items]
This will leak 'items' into the surrounding scope (but not 'x').
[x for x in x if x] # This works [x for y in x if x := y] # UnboundLocalError
(x for x in 5) # TypeError (x for _ in [1] for x in 5) # Works
I'm sure you can come up with more examples. The outermost iterable is special and magical.
ChrisA
- Previous message (by thread): [Python-Dev] PEP 572: Assignment Expressions
- Next message (by thread): [Python-Dev] PEP 572: Assignment Expressions
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]