[Python-ideas] Augmented assignment syntax for objects.

Chris Angelico rosuav at gmail.com
Fri Apr 28 03:31:01 EDT 2017


On Fri, Apr 28, 2017 at 5:02 PM, Random832 <random832 at fastmail.com> wrote:
> On Fri, Apr 28, 2017, at 01:30, Chris Angelico wrote:
>> Nothing whatsoever says that this is a good idea, but it's perfectly
>> legal, because the for loop is defined in terms of assignment. If this
>> were to be accepted (which, fwiw, I'm not actually advocating, but
>> IF), it would also be defined in terms of assignment. You still
>> shouldn't be assigning to arbitrary objects, especially not randomly
>> rebinding module names, but it's easy to grok the assignment
>> equivalence.
>
> What's not clear is when the left side (an object whose attribute/item
> is being assigned, and the item index) is evaluated, and why this should
> be different from when default arguments are evaluated.

Now that is a very good, dare I say it, argument. It's easy enough to
explain to an expert, but it's not clear to a beginner.

Expert explanation (omitting kwargs):

def __init__(self, self.spam=[]):

is roughly equivalent to:

_defaults = ([],)
def __init__(*args):
    self, self.spam = (args + _defaults)[:2]

It's pretty straight-forward for default arguments to be evaluated at
declaration time, but assignment targets at execution time; but it
isn't clear.

I was never really in favour of the proposal, but this is a fairly big downside.

ChrisA


More information about the Python-ideas mailing list