On Jun 20, 2019, at 13:25, nate lust <natel...@linux.com> wrote:
>
> There is nothing more humbling than sending nonsense out to an entire mailing
> list.
You’re something like the 300th person to suggest overloading assignment, but
the only one of that 300 willing to think it through, much less create a patch.
That’s hardly something to feel bad about!
> Thinking about things the right way around, I dug in to the interpreter an
> hacked something up to add an assignment overload dunder method. A diff
> against python 3.7 can be found here:
> https://gist.github.com/natelust/063f60a4c2c8ad0d5293aa0eb1ce9514
>
> There are other supporting changes, but the core idea is just that the type
> struct (typeobject.c) now has one more field (I called it tp_setself) that
> under normal circumstances is just 0. Then in the insertdict function
> (dictobject.c) (which does just what it sounds), post looking up the old
> value, and pre setting anything new, I added a block to check if tp_setself
> is defined on the type of the old value. If it is this means the user defined
> a __setself__ method on a type and it is called with the value that was to be
> assigned causing whatever side effects the user chose for that function, and
> the rest of insertdict body is never run.
Intercepting this at the namespace’s dict rather than at the store op is a
really clever idea, and it seems like it should avoid the major performance hit
as well. But I have a few questions.
Does assigning a local variable actually call dictinsert anywhere? There is a
locals dict, but I believe it’s only created and kept up to date with the fast
locals array if you ask for it.
What about other namespaces that aren’t dicts, like attributes of __slots__
classes?
What about dicts that aren’t namespaces? For example, will x[y] = z call
__setself__ on x[y] if x is a dict, but not if it’s a list (and if it’s a
SortedDict it may or may not depending on internal details of how that’s
implemented under the covers)?
Will x += y call __setself__(self) after the __iadd__(y)?
I’m not entirely sure what the right answer is for all of these situations. And
that might depend on how you describe this protocol in the docs (the name
“setself” doesn’t necessarily imply the same thing as “overloading assignment”).
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/GJZXLUNC66VUEYZPXFRKNNEE3LUOSWAC/
Code of Conduct: http://python.org/psf/codeofconduct/