python - Getting to parent property in subclasses that uses a baseclass -
i creating module need define bunch of objects used in same module.
my ultimate goal work;
m = mod() obj1 = m.t1('text') obj2 = m.t2(23) m.togheter(obj1, obj2)
the problem have need keep track of id in subclasses. i've gotten far, gives me attributeerror: 'super' object has no attribute '_cid'
return super(mod.modobj, self)._cid
class mod(object): current_cid = 0 @property def _cid(self): c = self.current_cid self.current_cid += 1 return c def togheter(self, obj1, obj2): # obj1.text , obj2.number here... return true class modobj(object): @property def _cid(self): return super(mod.modobj, self)._cid class t1(modobj): def __init__(self, text): self.text = text print self._cid class t2(modobj): def __init__(self, number): self.number = number print self._cid m = mod() m.t1('text') m.t2(23) print m.current_cid # should return 2
what wrong here? tried several other "trial , error" ways well, starting think doing wrong way...
modobj
doesn't inherit mod
; it's nested class inherits object
, , object
indeed not have property (or attribute) named _cid
.
Comments
Post a Comment