Defining class attributes + inheritance

Benjamin Kaplan benjamin.kaplan at case.edu
Tue Mar 8 18:11:41 EST 2011


On Mar 8, 2011 6:02 PM, "Martin De Kauwe" <mdekauwe at gmail.com> 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 and therefore can't inherit in this way,
> which defeats the purpose of defining a default base class. Am I being
> slow is there a nice solution to this or is that the way it works?
>
> thanks,
>
> Martin

Why does overriding the constructor make inheritance useless? You just have
to call the superclass's constructor, same as in every other OOP language
I've used. You can use *args and **kwargs to avoid relisting all the
superclass's arguements.

def __init__(self, newarg, *args, **kwargs):
    BaseClass.__init__(self, *args,**kwargs)
    self.newarg = newarg

> --
> http://mail.python.org/mailman/listinfo/python-list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110308/39768ee8/attachment.html>


More information about the Python-list mailing list