Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #29493

Re: Using dict as object

From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Subject Re: Using dict as object
Date 2012-09-19 14:04 +0000
References <345da32c-a22b-4025-bb50-794d684885b1@googlegroups.com> <5059B062.8020108@davea.name>
Newsgroups comp.lang.python
Message-ID <mailman.917.1348063463.27098.python-list@python.org> (permalink)

Show all headers | View raw


On 2012-09-19, Dave Angel <d@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Using dict as object Pierre Tardy <tardyp@gmail.com> - 2012-09-19 03:24 -0700
  Re: Using dict as object Dave Angel <d@davea.name> - 2012-09-19 07:45 -0400
  Re: Using dict as object Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-09-19 15:02 +0200
  Re: Using dict as object Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-09-19 14:04 +0000
  Re: Using dict as object Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-09-19 23:04 +0000

csiph-web