On Jun 19, 2019, at 16:57, Chris Angelico <[email protected]> wrote:
>
> On Thu, Jun 20, 2019 at 8:14 AM Andrew Barnert via Python-ideas
> <[email protected]> wrote:
>> … x = y would mean this:
>>
>> try:
>> xval = globals()['x']
>> result = xval.__iassign__(y)
>> except (LookupErrorr, AttributeError):
>> result = y
>> globals()['x'] = result
>>
>> ...
>> Notice that in my pseudocode above, I cheated—obviously the xval = and
>> result = lines are not supposed to recursively call the same pseudocode, but
>> to directly store a value in new temporary local variable.
>>
>
> I'm rather curious how this would behave in a class context. Consider
> the following code:
>
> num = 10; lst = [20, 30, 40]
> class Spam:
> num += 1
> lst += [50]
> print(num, lst, Spam.num, Spam.lst)
>
> Do you know what this will do in current Python, and what is your
> intention for this situation if we add a third name that uses the new
> __iassign__ protocol?
At least with CPython, I’m 99% sure that (unless you do something weird like
exec this class body with custom namespaces) you’ll get 10, [20,30,40,50], 11,
and [20,30,40,50]. The reason is that each += will compile to
LOAD_NAME/INPLACE_ADD/STORE_NAME, and LOAD_NAME will fall back to the global
when executed.
I’m not quite as sure that Python the language definition requires that
behavior, but I’d be a little surprised if any major implementation did
anything different.
So, I suppose this could be a problem:
sig = Signal() # a type that defines __iassign__
class Spam:
sig = 2
Here, the plausible alternative to calling sig.__iassign__(2) and then creating
a Spam.sig that binds the result (which should be identical to sig, as with lst
above) isn’t a NameError, but creating an Unrelated Spam.sig bound to 2. And
people might actually want or expect that.
I’m not sure which one people _would_ want or expect. The former is certainly
easier to implement, but that isn’t a good reason to choose it. I think we’d
need a realistic example of where this is actually useful before trying to
decide what it should do.
Anyway, since I’m proposing this idea specifically so it can be rejected, I
don’t need to come up with a good answer; the fact that it’s potentially
confusing In this context is just another reason to reject it, right? :)
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/FLKM3YO3YIFLUXNGZMU4NB5SMCNVVRD2/
Code of Conduct: http://python.org/psf/codeofconduct/