class S:
def __init__(self, **k): self.data = k
class E(S):
def __init__(self, **k):
S.__init__(self, **k)
x = E(a=1)
print x.data
{'a': 1}
From: [EMAIL PROTECTED]
To: python-list@python.org
Subject: extender method
Date: 26 Jul 2006 09:21:10 -0700
'Learning Python' by Lutz and Ascher (excellent book by the way)
explains that a subclass can call its superclass constructor as
follows:
class Super:
def method(self):
# do stuff
class Extender(Super):
def method(self):
Super.method(self) # call the method in super
# do more stuff - additional stuff here
I'm trying to use this for a superclass called 'component' in the
constructor. I have different types of component (let's say for
arguments sake resistor, capacitor etc). When I instantiate a new
resistor, say, I want the constructor to call the constructor within
the component superclass, and then add some resistor-specific stuff.
Now, this is fine using the above code. Where I'm struggling is with
argument passing. The following, for example, doesn't seem to work:
class Super:
def __init__(self, **kargs):
self.data = kargs
class Extender(Super):
def __init__(self, **kargs):
Super.__init__(self, kargs) # call the constructor method in Super
# do additional extender-specific stuff here
What am I doing wrong? I get:
TypeError: __init__() takes exactly 1 argument (2 given)
WARNING: Failure executing file: <main.py>
Dave
--
http://mail.python.org/mailman/listinfo/python-list
_________________________________________________________________
Is your PC infected? Get a FREE online computer virus scan from McAfee®
Security. http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
--
http://mail.python.org/mailman/listinfo/python-list