Augument assignment versus regular assignment

Terry Reedy tjreedy at udel.edu
Wed Jul 19 12:33:26 EDT 2006


"Antoon Pardon" <apardon at forel.vub.ac.be> wrote in message 
news:slrnebs290.ivs.apardon at rcpc42.vub.ac.be...
>> Try some personal introspection.  When you act as a Python interpreter,
>> what do you do?
>
> I do the evaluation of the target in the __setitem__ method
> (or the STORE_SUBSCR opcode).

I meant mentally, in your mind ;-)

>
> Let as look what the compilor makes of it:
> [blank lines added for clarity]
>
>>>> dis(compile("col['t'] = exp", '', 'single'))
>
>  1           0 LOAD_NAME                0 (exp)
>              3 LOAD_NAME                1 (col)
>              6 LOAD_CONST               0 ('t')
>
>              9 STORE_SUBSCR
>
> The bytecodes at 0, 3 and 6 don't do any evaluation, they
> just put things on the stack.
>
> So what does the STORE_SUBSCR at location 9 do (or __setitem__)?
>
> Well it will first do a number of preparations, like searching
> for a bucket in a dictionary or a node in a tree or list, may
> be even create one if a suitable wasn't available. And after
> that is done, the object will be somehow attached.
>
> So IMV those preparation before the attachment, belong to
> whatever the interpreter does before it actually attaches
> an object to a name/slot.
>
> So the evaluation of the target is part of what is done by
> STORE_SUBSCR or __setitem__.
>
> Now you can object to the fact that I have divided the work
> within an opcode.

But I won't.  The amount of duplication that can be factored out with 
augmented assignment depends on the granularity of operations.  And the 
granularity of operations depends on the interpreter.  Hence my claim that 
the details are necessarily interpreter dependent.

Terry Jan Reedy






More information about the Python-list mailing list