[Python-Dev] PEP 246, redux (original) (raw)

Phillip J. Eby pje at telecommunity.com
Wed Jan 12 21:03:28 CET 2005


At 05:02 PM 1/12/05 +0100, Alex Martelli wrote:

So, I think PEP 246 should specify that the step now called (e) [checking the registry] comes FIRST; then, an isinstance step [currently split between (a) and (d)], then conform and adapt steps [currently called (b) and (c)].

One question, and one suggestion.

The question: should the registry support explicitly declaring that a particular adaptation should not be used, thus pre-empting later phases entirely? This would allow for the possibility of speeding lookups by caching, as well as the option to "opt out" of specific adaptations, which some folks seem to want. ;)

The suggestion: rather than checking isinstance() in adapt(), define object.conform such that it does the isinstance() check. Then, Liskov violation is simply a matter of returning 'None' from conform instead of raising a special error.

Checking the registry is after all very fast: make the 2-tuple (type(obj), protocol), use it to index into the registry -- period. So, it's probably not worth complicating the semantics at all just to "fast path" the common case.

Okay, one more suggestion/idea:

$ timeit -s "d={}; d[1,2]=None" "d[1,2]" 1000000 loops, best of 3: 1.65 usec per loop

$ timeit -s "d={}; d[1]={2:None}" "d[1][2]" 1000000 loops, best of 3: 0.798 usec per loop

This seems to suggest that using nested dictionaries could be faster under some circumstances than creating the two-tuple to do the lookup. Of course, these are trivially-sized dictionaries and this is also measuring Python bytecode speed, not what would happen in C. But it suggests that more investigation might be in order.



More information about the Python-Dev mailing list