Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > extender method

Reply
Thread Tools

extender method

 
 
davehowey@f2s.com
Guest
Posts: n/a
 
      07-26-2006
'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

 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      07-26-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 'Learning Python' by Lutz and Ascher (excellent book by the way)
> explains that a subclass can call its superclass constructor as
> follows:
>

(snip)
>
> 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>
>


class Super(object):
def __init__(self, **kwargs):
self.data = kwargs

class Extender(Super):
def __init__(self, **kwargs):
Super.__init__(self, **kwargs)
# do additional extender-specific stuff here


HTH
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '(E-Mail Removed)'.split('@')])"
 
Reply With Quote
 
 
 
 
Chris Lambacher
Guest
Posts: n/a
 
      07-26-2006
On Wed, Jul 26, 2006 at 09:21:10AM -0700, (E-Mail Removed) wrote:
> '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

You mean
Super.__init__(self, **kargs)

Note the ** in the call the parent's init method.
> # 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

 
Reply With Quote
 
Simon Forman
Guest
Posts: n/a
 
      07-26-2006
(E-Mail Removed) wrote:
> '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


Try this:

class Extender(Super):
def __init__(self, **kargs):
Super.__init__(self, **kargs) # call the constructor method in
Super

(add two asterisks to the call.)

Observe, the following script:

def a(*a, **b):
return a, b

print a(**{'arg':2})
print a(arg=2)
print a({'arg':2})

# Prints:

((), {'arg': 2})
((), {'arg': 2})
(({'arg': 2},), {})


HTH,
~Simon

 
Reply With Quote
 
Paul McGuire
Guest
Posts: n/a
 
      07-26-2006
"Chris Lambacher" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Wed, Jul 26, 2006 at 09:21:10AM -0700, (E-Mail Removed) wrote:
> > '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
> >


With new-style classes (where Super inherits from object), I think the
preferred style is now:

super(Extender,self).__init__(**kwargs)

Instead of

Super.__init__(self,**kwargs)


class Super(object):
def __init__(self, **kargs):
print kargs
self.data = kargs

class Extender(Super):
def __init__(self, **kargs):
#~ Super.__init__(self, **kargs) # call the constructor method in
Super
super(Extender,self).__init__(**kargs)

e = Extender(a=123)

prints:
{'a': 123}

-- Paul


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
difference between Wifi repeater and range extender? AdminKen Wireless Networking 4 02-11-2011 04:05 AM
method def in method vs method def in block Kyung won Cheon Ruby 0 11-21-2008 08:48 AM
How change user name on Linksys Router and Range Extender? pattyjamas@hotmail.com Wireless Networking 4 04-27-2006 02:44 AM
Not connecting to DWL-G800AP Extender =?Utf-8?B?WFBOUlM=?= Wireless Networking 6 04-09-2005 09:55 PM
Media Extender for XBOX =?Utf-8?B?SmFtZXM=?= Wireless Networking 2 12-02-2004 10:13 AM



Advertisments