multi-Singleton-like using __new__

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Feb 8 21:10:15 EST 2008


On Sat, 09 Feb 2008 01:20:00 +0000, Matt Nordhoff wrote:

> Steven D'Aprano wrote:
>> Except that using has_key() means making an attribute lookup, which
>> takes time.
> 
> I was going to say that, but doesn't 'in' require an attribute lookup of
> some sort too, of __contains__ or whatever? 


I don't believe so. I understand that the in operator at the C level 
knows about built-in types, and does something like this pseudo-code:

case type(source):
    dict: return MAP_IN(source, target)
    list: return LIST_IN(source, target)
    str: return STR_IN(source, target)
otherwise:
    return source.__contains__(target)

Except all in C, so it's fast.

I understand that the dict has_key() method is essentially a wrapper 
around MAP_IN (or whatever it's actually called), and so is also fast, 
just not quite as fast because of the relatively slow look-up to get to 
has_key() in the first place.


-- 
Steven



More information about the Python-list mailing list