Defining class attributes + inheritance
Ethan Furman
ethan at stoneleaf.us
Tue Mar 8 18:20:05 EST 2011
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~
More information about the Python-list
mailing list