A question about inheritance
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: A question about inheritance
Thanks. Jp Calderone wrote: > On 8 May 2005 12:07:58 -0700, [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Modifying CallTips.py to work with with new-style classes in IDLE.
When using Idle, Call Tips are floating windows which display function, class, and method parameter and docstring information when you type an opening parenthesis, and which disappear when you type a closing parenthesis. But when you use new-style classes, they don't work fine and don't show the arguments of the __init__ method. There is a topic in this newsgroup (new style classes and an IDLE "grump"), where a solution was given to the problem (in Mar 27 2002): > The solution seems to be simply changing line 115 in CallTips.py (in the > Tools/Idle/ directory) as folows: > > if type(ob)==types.ClassType: > > to > > if type(ob)==types.ClassType or type(ob) == types.TypeType: I've tried it (Python 2.4.1 - IDLE 1.1.1) and it works fine (CallTips was in the \Lib\idlelib directory for me). Why isn't this the default behaviour? Is this the right place to post this? -- http://mail.python.org/mailman/listinfo/python-list
Re: A question about inheritance
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: > [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list