Defining class attributes + inheritance

Martin De Kauwe mdekauwe at gmail.com
Tue Mar 8 20:00:29 EST 2011


On Mar 9, 11:50 am, "Rhodri James" <rho... at wildebst.demon.co.uk>
wrote:
> On Wed, 09 Mar 2011 00:29:18 -0000, Martin De Kauwe <mdeka... at gmail.com>  
> wrote:
>
>
>
>
>
>
>
> > On Mar 9, 10:20 am, Ethan Furman <et... at stoneleaf.us> wrote:
> [snip]
> >> Just make sure and call the parent's constructor, either with
>
> >> class NewClass(BaseClass):
> >>      def __init__(self, ....):
> >>          BaseClass.__init__(self, other_params)
>
> >> or
>
> >> class NewClass(BaseClass):
> >>      def __init__(self, ....):
> >>          super(NewClass, self).__init__(....)
>
> >> ~Ethan~
>
> > Hi thanks, but I think I am implementing it wrong then?
>
> > BaseClass has 4 attributes and when I tried what you said
>
> > class NewClass(BaseClass):
> >     def __init__(self):
> >         super(NewClass, self).__init__(new_thing)
>
> > I get the error
>
> > TypeError: __init__() takes exactly 1 argument (6 given)
>
> Please give us either the rest of the code or the rest of the
> traceback, or preferably both.  Without one or the other we have
> little hope of guessing what you've typed.
>
> --
> Rhodri James *-* Wildebeest Herder to the Masses

OK

class BaseClass(object):

    def __init__(self, a, b, c, d):
        self.a = a
        self.b = b
        self.c = c
        self.d = d


class NewClass(BaseClass):
    def __init__(self):
        super(NewClass, self).__init__(new)
        self.new = new
        print self.new

class PreviousClass:
    def __init__(self, a, b, c, d, new):
        self.a = a
        self.b = b
        self.c = c
        self.d = d
        self.new = new
        print self.new


if __name__ == "__main__":

    A = PreviousClass(1, 2, 3, 4, 5)
    B = NewClass(1, 2, 3, 4, 5)

$ python test.py
Traceback (most recent call last):
  File "model_data.py", line 29, in <module>
    B = NewClass(1, 2, 3, 4, 5)
TypeError: __init__() takes exactly 1 argument (6 given)



So NewClass is my attempt to implement what I was shown and
PreviousClass was how I was originally solving the issue, i.e. I
wouldn't inherit the BaseClass.

thanks



More information about the Python-list mailing list