Two curious errors when function globals are manipulated

Random832 random832 at fastmail.com
Tue Jul 5 12:12:11 EDT 2016


On Tue, Jul 5, 2016, at 11:50, Steven D'Aprano wrote:
> If PyDict_SetItem expects an actual dict (accept no substitutes or
> subclasses), why is there no error? I would have expected a segfault at
> worst or at least an exception.

Subclasses are fine. Sort of. In general if you pass a *subclass* of
[say] dict to a built-in method that is expecting a real dict, the
effect is that the assignments will go straight through to the real
dict, and never call any overridden method. I.e. it's as if you called
dict.__setitem__(self, key, value). Something your own __setitem__
method may have done [directly or via super()] anyway... or may not
have. So any special logic in your own __setitem__, which may have
included e.g. copying it to an alternate place, changing the key or
value, or simply refusing to add the item to the dictionary at all, will
be ignored, and your object may end up in an inconsistent state.



More information about the Python-list mailing list