extender method

Discussion in 'Python' started by davehowey@f2s.com, Jul 26, 2006.

  1. Guest

    '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
     
    , Jul 26, 2006
    #1
    1. Advertising

  2. 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 ''.split('@')])"
     
    Bruno Desthuilliers, Jul 26, 2006
    #2
    1. Advertising

  3. On Wed, Jul 26, 2006 at 09:21:10AM -0700, 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
     
    Chris Lambacher, Jul 26, 2006
    #3
  4. Simon Forman Guest

    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
     
    Simon Forman, Jul 26, 2006
    #4
  5. Paul McGuire Guest

    "Chris Lambacher" <> wrote in message
    news:...
    > On Wed, Jul 26, 2006 at 09:21:10AM -0700, 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
     
    Paul McGuire, Jul 26, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven

    Extender Provider not saving state

    Steven, Nov 25, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    308
    Steven
    Nov 25, 2003
  2. JezB

    Extender Provider for ASP.NET ?

    JezB, Apr 19, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    379
  3. JezB

    Extender Provider

    JezB, Apr 30, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    396
  4. JezB
    Replies:
    0
    Views:
    341
  5. Phil Winstanley [Microsoft MVP ASP.NET]

    Re: Create custom properties with extender provider ?

    Phil Winstanley [Microsoft MVP ASP.NET], Jun 14, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    355
Loading...

Share This Page