oop - basic inheritance (in python) -
users,
i have basic question concerning inheritance (in python). have 2 classes , 1 of them inherited other like
class p: def __init__(self,name): self.pname = name class c(p): def __init__(self,name): self.cname = name
is there possibility can create parent object , several child objects refer same parent object? should work that parent object contains several variables , whenever access corresponding variables child access variable form parent. i.e. if change 1 child changed other childes , data stored once in memory (and not copied each child...)
thank in advance.
here possible workaround not consider nice
class p: def __init__(self, name): self.pname = name class c: def __init__(self, name,pobject): self.pobject = pobject self.cname = name
is state of art or there exist other concepts?
sebastian
thank helping me, name conventions :) still not satisfied. maybe give more advanced example stress want do.
class p: data = "shareddata" def __init__(self,newdata): self.data = newdata def printname(self): print self.name class c(p): def __init__(self,name): self.name = name
now can following
in [33]: c1 = test.c("name1") in [34]: c2 = test.c("name2") in [35]: c1.printname() name1 in [36]: c2.printname() name2 in [37]: c1.data out[37]: 'shareddata' in [38]: c2.data out[38]: 'shareddata'
and far want. there variable name different every child , parent class accesses individual variables. normal inheritance. there variable data comes parent class , every child access it. however, following not work more
in [39]: c1.data = "tst" in [40]: c2.data out[40]: 'shareddata' in [41]: c1.data out[41]: 'tst'
i want change in c1.data affect c2.data since want variable shared, somehow global variable of parent class.
and more that. want create different instances of p, each having own data variable. , when create new c object want specify p object data should inhetited i.e. shared....
update:
remark comment of @eyquem: this, going direction want. however, __class__.pvar
shared among objects of class. want several instances of p may have different pvar. lets assume p1 has pvar=1 , p2 has pvar=2. want create children c1a, c1b, c1c related p1, i.e. if c1a.pvar should acess pvar p1. create c2a, c2b, c2c , if access i.e. c2b.pvar want access pvar p2. since class c inherits pvar class p pvar known c. naive idea if create new instance of c should able specify (existing) p object should used parent object , not create new p object done when calling p.__init__
inside of __init__
of c... sounds simple me, maybe forget something...
update:
so found this discussion pretty question
any suggestions?
update:
the method .class._subclasses_ seems not existing more..
update:
here onother link:
there solved copying. not want copy parent class since exists once...
update:
sorry leaving discussion yesterday, bit ill... , thank posts! read through them. thought bit more , here possible solution found
class p(object): def __init__(self,pvar): self.pobject = none self._pvar = pvar @property def pvar(self): if self.pobject != none: return self.pobject.pvar else: return self._pvar @pvar.setter def pvar(self,val): if self.pobject != none: self.pobject.pvar = val else: self._pvar=val def printname(self): print self.name class c(p): def __init__(self,name,pobject): #<-- same default `p()` # used instances of `c`, # unless pobject explicitly defined. p.__init__(self,none) self.name = name self.pobject = pobject p1 = p("1") p2 = p("2") c1a = c("c1a",p1) c1b = c("c1b",p1) c1c = c("c1c",p1) c2a = c("c2a",p2) c2b = c("c2b",p2) c2c = c("c2c",p2) print id(c1a.pvar) print id(c1b.pvar) print id(c1c.pvar) print id(c2a.pvar) print id(c2b.pvar) print id(c2c.pvar) print id(p1.pvar) print id(p2.pvar) print id(c1a.name) print id(c1b.name) print id(c1c.name) print id(c2a.name) print id(c2b.name) print id(c2c.name)
it bit cumbersome , hope there simpler way achieve this. has feature pvar mentioned in class p , class c not know pvar should according understanding of inheritance. nevertheless when create new instance of c can specify existing instance of p stored in variable pobject. when variable pvar accessed pvar of p-instance stored in variable accessed...
the output given by
3078326816 3078326816 3078326816 3074996544 3074996544 3074996544 3078326816 3074996544 156582944 156583040 156583200 156583232 156583296 156583360
i read through last comments,
all best, sebastian
update:
i think elegant way following (which not work)
class p(object): def __init__(self,pvar): self.pvar = pvar def printname(self): print self.name class c(p): def __init__(self,name,pobject): p = pobject self.name = name
i think python should allow this...
update:
ok, found way achieve this, due explanations eyquem. since hack there should official version same...
def replaceinstance(parent,child): item in parent.__dict__.items(): child.__dict__.__setitem__(item[0],item[1]) print item class p(object): def __init__(self,pvar): self.pvar = pvar def printname(self): print self.name class c(p): def __init__(self,name,pobject): p.__init__(self,none) replaceinstance(pobject,self) self.name = name p1 = p("1") p2 = p("2") c1a = c("c1a",p1) c1b = c("c1b",p1) c1c = c("c1c",p1) c2a = c("c2a",p2) c2b = c("c2b",p2) c2c = c("c2c",p2) print id(c1a.pvar) print id(c1b.pvar) print id(c1c.pvar) print id(c2a.pvar) print id(c2b.pvar) print id(c2c.pvar) print id(p1.pvar) print id(p2.pvar) print id(c1a.name) print id(c1b.name) print id(c1c.name) print id(c2a.name) print id(c2b.name) print id(c2c.name)
the output same above
3077745184 3077745184 3077745184 3074414912 3074414912 3074414912 3077745184 3074414912 144028416 144028448 144028480 144028512 144028544 144028576
update: if id's seem right, last code not work clear test
c1a.pvar = "newpvar1" print c1a.pvar print c1b.pvar print c1c.pvar print c2a.pvar print c2b.pvar print c2c.pvar print p1.pvar print p2.pvar
it has output
newpvar1 1 1 2 2 2 1 2
however version posted first works:
class p(object): def __init__(self,pvar): self.pobject = none self._pvar = pvar @property def pvar(self): if self.pobject != none: return self.pobject.pvar else: return self._pvar @pvar.setter def pvar(self,val): if self.pobject != none: self.pobject.pvar = val else: self._pvar=val def printname(self): print self.name class c(p): def __init__(self,name,pobject): #<-- same default `p()` # used instances of `c`, # unless pobject explicitly defined. p.__init__(self,none) self.name = name self.pobject = pobject p1 = p("1") p2 = p("2") c1a = c("c1a",p1) c1b = c("c1b",p1) c1c = c("c1c",p1) c2a = c("c2a",p2) c2b = c("c2b",p2) c2c = c("c2c",p2) print id(c1a.pvar) print id(c1b.pvar) print id(c1c.pvar) print id(c2a.pvar) print id(c2b.pvar) print id(c2c.pvar) print id(p1.pvar) print id(p2.pvar) print id(c1a.name) print id(c1b.name) print id(c1c.name) print id(c2a.name) print id(c2b.name) print id(c2c.name) print "testing\n" c1a.printname() c1b.printname() c1c.printname() c2a.printname() c2b.printname() c2c.printname() print "\n" c1a.name = "c1anewname" c2b.name = "c2bnewname" c1a.printname() c1b.printname() c1c.printname() c2a.printname() c2b.printname() c2c.printname() print "pvar\n" print c1a.pvar print c1b.pvar print c1c.pvar print c2a.pvar print c2b.pvar print c2c.pvar print p1.pvar print p2.pvar print "\n" c1a.pvar = "newpvar1" print c1a.pvar print c1b.pvar print c1c.pvar print c2a.pvar print c2b.pvar print c2c.pvar print p1.pvar print p2.pvar print "\n" c2c.pvar = "newpvar2" print c1a.pvar print c1b.pvar print c1c.pvar print c2a.pvar print c2b.pvar print c2c.pvar print p1.pvar print p2.pvar
with output
3077745184 3077745184 3077745184 3074414912 3074414912 3074414912 3077745184 3074414912 144028416 144028448 144028480 144028512 144028544 144028576 testing c1a c1b c1c c2a c2b c2c c1anewname c1b c1c c2a c2bnewname c2c pvar 1 1 1 2 2 2 1 2 newpvar1 newpvar1 newpvar1 2 2 2 newpvar1 2 newpvar1 newpvar1 newpvar1 newpvar2 newpvar2 newpvar2 newpvar1 newpvar2
does know why that? not understand internal way python works __dict__
well...
it should work that parent object contains several variables , whenever access corresponding variables child access variable form parent. i.e. if change 1 child changed other childes , data stored once in memory (and not copied each child...)
that's not inheritance.
that's different concept.
your "shared variables" objects can mutated , have references in other objects. nothing interesting.
inheritance different this.
Comments
Post a Comment