[Python-Dev] Meta-reflections (original) (raw)
Samuele Pedroni pedroni@inf.ethz.ch
Fri, 22 Feb 2002 01:38:27 +0100
- Previous message: [Python-Dev] Meta-reflections
- Next message: [Python-Dev] Meta-reflections
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
From: Tim Peters <tim.one@comcast.net>
[Tim] > Slots were definitely intended as a memory optimization, and the ways in > which they don't act like "regular old attributes" are at best warts.
[Samuele Pedroni] > I see, but it seems that the only way to coherently and transparently > remove the warts implies that the dict of a new-style class > instance with slots should be tied with the instance and cannot > be anymore a vanilla dict. Something only Guido can rule about. He'll be happy to . Optimizations aren't always wart-free, and then living with warts is a price paid for benefiting from the optimization. I'm sure Guido would consider it "a bug" if slots are ignored by the pickling mechanism, but wouldn't for an instant consider it "a bug" that the set of slots in effect when a class is created can't be dynamically expanded later (this latter is more a sensible restriction than a wart, IMO -- and likely in Guido's too).
I was thinking along the line of the C equiv of this: [Yup the situation of a subclass of a class with slots is more relevant]
class C(object): slots = ['_a']
class D(C): pass
def allslots(cls): mro = list(cls.mro) mro.reverse() allslots = {} for c in mro: cdict = c.dict if 'slots' in cdict: for slot in cdict['slots']: allslots[slot] = cdict[slot] return allslots
class slotdict(dict): slots = ['_inst','_allslots'] def init(self,inst,allslots): self._inst = inst self._allslots = allslots
def getitem(self,k): if self._allslots.has_key(k): # self _allslots should be reachable as self._inst.class.allslots # AttributeError should become a KeyError ? return self._allslots[k].get(self._inst) else: return dict.getitem(self,v)
def setitem(self,k,v): if self._allslots.has_key(k): # self _allslots should be reachable as self._inst.class.allslots # AttributeError should become a KeyError ? return self._allslots[k].set(self._inst,v) else: return dict.setitem(self,v)
other methods accordingly
d=D() d.dict = slotdict(d,allslots(D)) # should be so automagically
allslots(D) should be probably accessible as d.class.allslots
for transparency C.dict should not contain any slot descr
allslots should be readonly and disallow rebinding
d.dict should disallow rebinding
c =C() ; c.dict should return a proxy dict lazily or even more so ...
Lots of things to rule about and trade-offs to consider.
the-more-it's-arbitrary-the-more-you-need-one-ruler-ly y'rs - Samuele.
- Previous message: [Python-Dev] Meta-reflections
- Next message: [Python-Dev] Meta-reflections
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]