Re: [Python-Dev] The `for y in [x]` idiom in comprehensions

2018年2月23日 20:44:36 -0800

Chris Barker writes:
 > But I still think the original:
 > 
 > [g(y) for x in range(5) for y in [f(x)]]
 > 
 > Is always going to be confusing to read.
But the point I was making with "def f(x=[0]):" was this: you have a
situation where your desired semantics is "value of some type"[1], but
the language's syntax doesn't permit a value of that type there, while
"singleton sequence of that type" works fine.
In fact, "singleton as value" is baked into Python in the form of
str.__getitem__ and bytes.__getitem__. So we now have four use cases
for singleton as value: two stringish actual types, and the two idioms
"mutable default argument" and "local variable in comprehension". The
horse is long since out of the barn.
Steve
Footnotes: 
[1] Both "value" and "type" are used rather loosely here.
_______________________________________________
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to