[Python-Dev] A new dictionary implementation (original) (raw)
Hans Mulder hansmu at xs4all.nl
Wed Feb 1 18:13:19 CET 2012
- Previous message: [Python-Dev] Python 3 optimizations, continued, continued again...
- Next message: [Python-Dev] A new dictionary implementation
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Previous message: [Python-Dev] Python 3 optimizations, continued, continued again...
- Next message: [Python-Dev] A new dictionary implementation
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]