[Persistence-sig] "Straw Baby" Persistence API
Jeremy Hylton
jeremy@alum.mit.edu
Mon, 22 Jul 2002 11:02:11 -0400
>>>>> "SM" == Steve Menard <smenard@bigfoot.com> writes:
GvR> But perhaps these should be rewritten to derive from dict and
GvR> list instead of UserDict and UserList?
>>
>> One small comment. (I owe more substantial comment on Phillip's
>> earlier proposals.) The persistent versions of dict and list
>> can't extend the builtin types, because they need to hook
>> __getitem__() and __setitem__(). The overridden methods may not
>> be called if we extend the builtin types.
>>
SM> hum, if those method are not guaranteed to be called by
SM> subclassing dict or list, then there is something broken. Either
SM> that or there is a subtle thing I do not understand.
The latter. For performance reasons, most C code uses calls like
PyDict_GetItem(), which operates directly on the C representation of a
dict. If you inherit from dict, you'll get the same C representation
for your object. That allows PyDict_GetItem() to be called, but
doesn't arrange to call your __getitem__() method. The indirection
required to invoke a subclass's __getitem__() would cause serious
performance problems.
Normally Guido only recommends inheriting from dict to add new
behavior (as opposed to customizing existing behavior).
Jeremy