[Python-ideas] On evaluating features [was: Unpacking iterables for augmented assignment]

Chris Angelico rosuav at gmail.com
Thu Sep 6 14:46:36 EDT 2018

On Fri, Sep 7, 2018 at 4:38 AM, Franklin? Lee
<leewangzhong+python at gmail.com> wrote:
> The following are equivalent and compile down to the same code:
>     a, b, c = lst
>     [a, b, c] = lst
> The left hand side is not an actual list (even though it looks like
> one). The brackets are optional. The docs call the left hand side a
> target list: https://docs.python.org/3/reference/simple_stmts.html#assignment-statements
> "Target list" is not a real type. You can't construct such an object,
> or hold one in memory. You can't make a class that emulates it
> (without interpreter-specific hacks), because it is a collection of
> its names, not a collection of values.

A target list is a syntactic element, like a name, or an operator, or
a "yield" statement. You can't construct one, because it isn't an
object type. It's not a "virtual type". It's a completely different
sort of thing.

> target_list.__iadd__ also does not exist, because target_list does not
> exist. However, target_list can be thought of as a virtual type, a
> type that the compiler compiles away. We can then consider
> target_list.__iadd__ as a virtual operator, which the compiler will
> understand but hide from the runtime.
> I was making the point that, because the __iadd__ in the example does
> not refer to list.__iadd__, but rather a virtual target_list.__iadd__,
> there is not yet a violation of the rule.

What you're suggesting is on par with trying to say that:

for += 5

should be implemented as:


where "current_loop" doesn't really exist, but it's a virtual type
that represents a 'for' loop. That doesn't make sense, because there
is no object in Python to represent the loop. There is no class/type
that represents all loops, on which a method like this could be added.
The word 'for' is part of the grammar, not the object model. And
"target list" is the same. There's no way to attach an __iadd__ method
to something that doesn't exist.

So for your proposal to work, you would need to break that rule, and
give a *different* meaning to this.


More information about the Python-ideas mailing list