Using dict as object

Oscar Benjamin oscar.j.benjamin at gmail.com
Wed Sep 19 10:04:07 EDT 2012


On 2012-09-19, Dave Angel <d at davea.name> wrote:
> On 09/19/2012 06:24 AM, Pierre Tardy wrote:
>> All implementation I tried are much slower than a pure native dict access.
>> 
 Each implementation have bench results in commit comment. All of them
>> are 20+x slower than plain dict!
>
> Assuming you're talking about CPython benchmarks, the dict is highly
> optimized, C code.  And when you provide your own __getitem__
> implementation in pure python, there are many attribute accesses, just
 to
> make the code work.
>
>> I would like to have python guys advices on how one could optimize this.
>

I agree with all of Dave's objections to this idea. It is possible, however,
to make a more efficient implementation than the one that you have:

class Namespace(dict):
    def __init__(self, *args, **kwargs):
        dict.__init__(self, *args, **kwargs)
        self.__dict__ = self

This implementation is not really sane, though, as it doesn't hide any of the
dict methods as attributes. It does, however, demonstrate something that be a
potentially simple way of making an alternate type object in C.

Oscar




More information about the Python-list mailing list