Defining class attributes + inheritance

  • Thread starter Martin De Kauwe
  • Start date
M

Martin De Kauwe

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
 
E

Ethan Furman

Martin said:
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~
 
M

Martin De Kauwe

Martin said:
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)
 
J

James Mills

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__(....)

In Python3 this is even easier (for the simplest case):
.... def __init__(self, x):
.... print("Hello %s" % x)
........ def __init__(self, x, y):
.... super().__init__(x)
.... print("Hello %s" % y)
....Hello foo
Hello bar
cheers
James
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top