inheriting variables
Haran Shivanan
hs2l2ATyahoodotcom at yahoo.com
Thu Apr 17 06:34:08 EDT 2003
This seems like a fairly straight forward thing but I'm not sure about how
to implement it.
I have a base class from which I'm deriving several new classes:
class A:
mem_a=0
mem_b=1
class B(A):pass
I want B to inherit mem_a and mem_b from A without my having to explicit do
something like:
self.mem_a = 0
in the constructor for class B.
Basically, when I want to add a membership variable to all classes that
inherit from A, and I want to do it without manually editing all the class
definitions\constructors.
Currently I'm doing:
class B(A):
def __init(self):
for k in A.__dict__.keys():
self.__dict__[k] = A.__dict__[k]
...
This is somewhat crude. Also it does wierd things with methods declared in
A. Is there some really obvious clean implementation I'm missing?
Thanks.
--
Happiness is not a fish you can catch
More information about the Python-list
mailing list