A question about inheritance

Discussion in 'Python' started by arserlom@gmail.com, May 8, 2005.

  1. Guest

    Hello I have a question about inheritance in Python. I'd like to do
    something like this:

    class cl1:
    def __init__(self):
    self.a = 1

    class cl2(cl1):
    def __init__(self):
    self.b = 2

    But in such a way that cl2 instances have atributes 'b' AND 'a'.
    Obviously, this is not the way of doing it, because the __init__
    definition in cl2 overrides cl1's __init__.

    Is there a 'pythonic' way of achieving this?

    Armando Serrano
     
    , May 8, 2005
    #1
    1. Advertising

  2. Guest

    Thanks.

    Jp Calderone wrote:
    > On 8 May 2005 12:07:58 -0700, wrote:
    > >Hello I have a question about inheritance in Python. I'd like to do
    > >something like this:
    > >
    > > class cl1:
    > > def __init__(self):
    > > self.a = 1
    > >
    > > class cl2(cl1):
    > > def __init__(self):
    > > self.b = 2
    > >
    > >But in such a way that cl2 instances have atributes 'b' AND 'a'.
    > >Obviously, this is not the way of doing it, because the __init__
    > >definition in cl2 overrides cl1's __init__.
    > >
    > >Is there a 'pythonic' way of achieving this?

    >
    > class cl2(cl1):
    > def __init__(self):
    > cl1.__init__(self)
    > self.b = 2
    >
    > Jp
     
    , May 8, 2005
    #2
    1. Advertising

  3. wrote:
    > Hello I have a question about inheritance in Python. I'd like to do
    > something like this:
    >
    > class cl1:
    > def __init__(self):
    > self.a = 1
    >
    > class cl2(cl1):
    > def __init__(self):
    > self.b = 2
    >
    > But in such a way that cl2 instances have atributes 'b' AND 'a'.
    > Obviously, this is not the way of doing it, because the __init__
    > definition in cl2 overrides cl1's __init__.
    >
    > Is there a 'pythonic' way of achieving this?


    If there's a chance you might have multiple inheritance at some point in
    this hierarchy, you might also try using super:

    class cl1(object): # note it's a new-style class
    def __init__(self):
    self.a = 1

    class cl2(cl1):
    def __init__(self):
    super(cl2, self).__init__()
    self.b = 2

    Note that you probably want a new-style class even if you chose not to
    use super in favor of Jp Calderone's suggestion. There are very few
    cases for using old-style classes these days.

    STeVe
     
    Steven Bethard, May 8, 2005
    #3
  4. Guest

    Ok, thanks. I didn't know about new-style classes (I had learned python
    from a book prior to them).

    After reading about new-style classes, I find that your solution is
    better because, using super (in general) avoids cl2 from having to know
    the implementation details of cl1. This is clearly explained in:

    http://www.python.org/2.2.3/descrintro.html#cooperation

    Also, when using new-style classes with IDLE, I found some problems
    which I talk about in "Modifying CallTips.py to work with with
    new-style classes in IDLE.", which I posted in this group.

    Steven Bethard wrote:
    > wrote:
    > > Hello I have a question about inheritance in Python. I'd like to do
    > > something like this:
    > >
    > > class cl1:
    > > def __init__(self):
    > > self.a = 1
    > >
    > > class cl2(cl1):
    > > def __init__(self):
    > > self.b = 2
    > >
    > > But in such a way that cl2 instances have atributes 'b' AND 'a'.
    > > Obviously, this is not the way of doing it, because the __init__
    > > definition in cl2 overrides cl1's __init__.
    > >
    > > Is there a 'pythonic' way of achieving this?

    >
    > If there's a chance you might have multiple inheritance at some point

    in
    > this hierarchy, you might also try using super:
    >
    > class cl1(object): # note it's a new-style class
    > def __init__(self):
    > self.a = 1
    >
    > class cl2(cl1):
    > def __init__(self):
    > super(cl2, self).__init__()
    > self.b = 2
    >
    > Note that you probably want a new-style class even if you chose not

    to
    > use super in favor of Jp Calderone's suggestion. There are very few
    > cases for using old-style classes these days.
    >
    > STeVe
     
    , May 14, 2005
    #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. maxw_cc
    Replies:
    1
    Views:
    3,159
    Martijn van Steenbergen
    Dec 21, 2003
  2. cppsks
    Replies:
    0
    Views:
    833
    cppsks
    Oct 27, 2004
  3. karthikbalaguru
    Replies:
    9
    Views:
    1,053
  4. Daniel Pitts
    Replies:
    27
    Views:
    1,924
    Mike Schilling
    Feb 27, 2008
  5. johnsonlau
    Replies:
    1
    Views:
    783
    Kai-Uwe Bux
    Jul 21, 2008
Loading...

Share This Page