python - How to access attribute of superclass instance from child class method -
i have following class structure in code:
superclass, a, list of objects subclass, b. class has method, meth, creates variable, var, (by reading file). variable, var, needs accessible method of subclass b. problem after initialising = a(), var attribute of instance a, can't accessed subclass methods using super(). var large array of floats (around 1e5 elements), i'm trying minimise memory usage not making attribute of instance of b (n times!) or passing explicitly methb.
class a(object): def __init__(self,n): self.data = [b() _ in xrange(n)] def __getitem__(self,n): return self.data[n] def metha(self): open('file.txt','r') fp: self.var = fp.readlines() class b(a): def __init__(self): self.derived_var = [] def methb(): '''this method needs use var defined in meth of above.''' = a(5) a.metha() b in a: b.methb()
is possible in python or bad coding??
first of don't see point of defining b
subclass of a
. don't seem share functionalites , create circular reference between b
, a
(in constructor of a
) might lead problems.
now question. problem pass a.var
methb
? this:
class b(a): def __init__(self): self.derived_var = [] def methb(self, var): # code = a(5) a.meth() b in a: b.methb(a.var)
Comments
Post a Comment