Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > A question about inheritance

Reply
Thread Tools

A question about inheritance

 
 
arserlom@gmail.com
Guest
Posts: n/a
 
      05-08-2005
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

 
Reply With Quote
 
 
 
 
arserlom@gmail.com
Guest
Posts: n/a
 
      05-08-2005
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


 
Reply With Quote
 
 
 
 
Steven Bethard
Guest
Posts: n/a
 
      05-08-2005
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
 
Reply With Quote
 
arserlom@gmail.com
Guest
Posts: n/a
 
      05-14-2005
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Struct inheritance against class inheritance johnsonlau C++ 1 07-21-2008 04:58 PM
Interface inheritance vs Implementation inheritance. Daniel Pitts Java 27 02-27-2008 01:37 AM
Private Inheritance and Publice Inheritance karthikbalaguru C++ 9 09-10-2007 01:05 PM
mul. inheritance & overloading operator new/delete solved by virtual base inheritance? cppsks C++ 0 10-27-2004 07:49 PM
Private access modifier and Inheritance (Inheritance implementation in Java) maxw_cc Java 1 12-21-2003 11:38 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57