On 10/19/2019 07:56 PM, Steve Jorgensen wrote:

When using a custom classdict to implement a DSL or use in the body of a class 
definition, from what I can tell by experiment, the classdict takes priority, 
and the surrounding context is only referenced if trying to get an item from 
the classdict raises `KeyError`.

Thank you for the examples.

As you have discovered, the issue is that when Python is processing a class and 
gets a name (whether a def, a property, a variable, etc., is irrelevant) it 
tries to look that name up in the class_dict that was returned by __prepare__, 
and that class_dict has one shot at it -- either return an object for the name, 
or raise KeyError -- in which case Python will keep looking up the scope in an 
attempt to find a match.

Having said all that, I'm now wondering if wrapping a super() call in a 
try/except inside class_dict.__get_item__ might solve the problem -- although, 
even if that worked, you would have the reverse problem of wanting a name to be 
new in your class, but already existing outside.

The solution I use in aenum.Enum is an extra variable, _ignore_, that lists all 
names that shouldn't be shadowed in the new dict; it starts as being set to 
property, classmethod, and staticmethod, and the user can replace that list 
with whatever they like.

--
~Ethan~
_______________________________________________
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/LEKGEOCUNHPGA2BTXZJEMBVIHIEAHZLC/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to