[Python-Dev] A new dictionary implementation

Hans Mulder hansmu at xs4all.nl
Wed Feb 1 18:13:19 CET 2012


On 30/01/12 00:30:14, Steven D'Aprano wrote:
> Mark Shannon wrote:
>> Antoine Pitrou wrote:
[......]
>> Antoine is right. It is a reorganisation of the dict, plus a couple of
>> changes to typeobject.c and object.c to ensure that instance
>> dictionaries do indeed share keys arrays.
>
>
> I don't quite follow how that could work.
>
> If I have this:
>
> class C:
> pass
>
> a = C()
> b = C()
>
> a.spam = 1
> b.ham = 2
>
>
> how can a.__dict__ and b.__dict__ share key arrays? I've tried reading
> the source, but I'm afraid I don't understand it well enough to make
> sense of it.

They can't.

But then, your class is atypical.  Usually, classes initialize all the
attributes of their instances in the __init__ method, perhaps like so:

class D:
     def __init__(self, ham=None, spam=None):
         self.ham = ham
         self.spam = spam

As long as you follow the common practice of not adding any attributes
after the object has been initialized, your instances can share their
keys array.  Mark's patch will do that.

You'll still be allowed to have different attributes per instance, but
if you do that, then the patch doesn't buy you much.

-- HansM






More information about the Python-Dev mailing list