Defining class attributes + inheritance

Martin De Kauwe mdekauwe at gmail.com
Tue Mar 8 19:29:18 EST 2011


On Mar 9, 10:20 am, Ethan Furman <et... at stoneleaf.us> wrote:
> Martin De Kauwe wrote:
> > Hi,
>
> > I think this might be obvious? I have a base class which contains X
> > objects which other classes inherit e.g.
>
> > class BaseClass(object):
> >     def __init__(self, something, something_else):
> >         self.something = something
> >         self.something_else = something_else
> >         # etc
>
> > Typically I would use this like this
>
> > from some_module_name import BaseClass
>
> > class NewClass(BaseClass):
> >     def do_something(self):
> >          print self.something
> >          # etc
>
> > Which is fine. However if I need to inherit additional attributes (to
> > NewClass) at the constructor step it means I have to completely
> > redefine the constructor [...]
>
> 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)



More information about the Python-list mailing list