meaning of: line, =

Chris Angelico rosuav at gmail.com
Thu Feb 5 20:17:31 EST 2015


On Fri, Feb 6, 2015 at 12:12 PM, Devin Jeanpierre
<jeanpierreda at gmail.com> wrote:
> Here's another example, one that still exists in Python 3:
>
>>>> [] = ''
>>>> () = ''
>   File "<stdin>", line 1
> SyntaxError: can't assign to ()
>
> The syntax explicitly blacklists (), but forgets to blacklist [].

So... this is actually a really really obscure little feature.

[] = x
# is equivalent to
try: next(iter(x))
except StopIteration: pass
else: raise ValueError("too many values to unpack (expected 0)")

It's a way of asserting that an iterator is exhausted! Perfect code
snippet for your next International Obfuscated Python Code Contest
entry.

ChrisA



More information about the Python-list mailing list