Defining class attributes + inheritance

Discussion in 'Python' started by Martin De Kauwe, Mar 8, 2011.

  1. 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
     
    Martin De Kauwe, Mar 8, 2011
    #1
    1. Advertising

  2. Martin De Kauwe

    Ethan Furman Guest

    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~
     
    Ethan Furman, Mar 8, 2011
    #2
    1. Advertising

  3. On Mar 9, 10:20 am, Ethan Furman <> 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)
     
    Martin De Kauwe, Mar 9, 2011
    #3
  4. Martin De Kauwe

    James Mills Guest

    On Wed, Mar 9, 2011 at 9:20 AM, Ethan Furman <> wrote:
    > 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):

    >>> class Base:

    .... def __init__(self, x):
    .... print("Hello %s" % x)
    ....
    >>> class ExtendedBase(Base):

    .... def __init__(self, x, y):
    .... super().__init__(x)
    .... print("Hello %s" % y)
    ....
    >>> x = ExtendedBase("foo", "bar")

    Hello foo
    Hello bar
    >>>


    cheers
    James

    --
    -- James Mills
    --
    -- "Problems are solved by method"
     
    James Mills, Mar 9, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. johny smith
    Replies:
    8
    Views:
    420
    Peter Koch Larsen
    Jul 2, 2004
  2. Xiangliang Meng
    Replies:
    5
    Views:
    815
    ThosRTanner
    Apr 12, 2005
  3. james_027

    class attributes & data attributes

    james_027, Jun 20, 2007, in forum: Python
    Replies:
    2
    Views:
    377
    Bruno Desthuilliers
    Jun 20, 2007
  4. Thomas
    Replies:
    4
    Views:
    173
    Thomas
    Jun 7, 2005
  5. Carbon Monoxide
    Replies:
    3
    Views:
    121
    Carbon Monoxide
    May 31, 2008
Loading...

Share This Page