Re: Best practices for using super()

2023-07-05 Thread Lars Liedtke via Python-list
Hey, When you have multiple inheritance and you e.g. want to explicitely call __init__ of one of the classes inherited from, that is not the first in the list of classes to inherit from. Cheers, Lars Lars Liedtke Senior Software Developer [Tel.] +49 721 98993- [Fax] +49 721 98993- [E-Ma

Best practices for using super()

2023-07-04 Thread Peter Slížik via Python-list
As a follow-up to my yesterday's question - are there any recommendations on the usage of super()? It's clear that super() can be used to invoke parent's: - instance methods - static methods - constants ("static" attributes in the parent class, e.g. super().NUMBER). This all works, but are the

Re: Multiple inheritance using super() in parent classes

2022-02-10 Thread Peter Otten
On 10/02/2022 09:20, Igor Basko wrote: Hi everyone, This is my first question here. Hope to get some clarification. Basically this question is about multiple inheritance and the usage of super().__init__ in parent classes. So I have two classes that inherit from the same base class. For example

Multiple inheritance using super() in parent classes

2022-02-10 Thread Igor Basko
Hi everyone, This is my first question here. Hope to get some clarification. Basically this question is about multiple inheritance and the usage of super().__init__ in parent classes. So I have two classes that inherit from the same base class. For example class B and class C inherit from A: class

Re: using super

2010-01-19 Thread Jean-Michel Pichavant
Gabriel Genellina wrote: I see. Then is there a reason why return super(Subclass, self).parrot() would be prefered over the "classic" return Base.parrot(self) ? Or is it just a matter of preference ? For a longer explanation, see: James Knight: Python's Super Considered Harmful http://fuhm

Re: using super

2010-01-18 Thread Gabriel Genellina
En Mon, 18 Jan 2010 16:50:45 -0300, Jean-Michel Pichavant escribió: Duncan Booth wrote: Jean-Michel Pichavant wrote: class SubClass(Base): colour = "Red" def parrot(self): """docstring for Subclass""" return super(Subclass, self).parrot() I'm not a big fan of supe

Re: using super

2010-01-18 Thread Arnaud Delobelle
Jean-Michel Pichavant writes: [...] > Then is there a reason why >>>> return super(Subclass, self).parrot() > would be prefered over the "classic" >>>> return Base.parrot(self) > ? > > Or is it just a matter of preference ? Using super() calls

Re: using super

2010-01-18 Thread Jean-Michel Pichavant
Duncan Booth wrote: Jean-Michel Pichavant wrote: class SubClass(Base): colour = "Red" def parrot(self): """docstring for Subclass""" return super(Subclass, self).parrot() I'm not a big fan of super, but I'm still wondering if return super(self.__cl

Re: using super

2010-01-18 Thread Duncan Booth
Jean-Michel Pichavant wrote: > class SubClass(Base): colour = "Red" def parrot(self): """docstring for Subclass""" return super(Subclass, self).parrot() > I'm not a big fan of super, but I'm still wondering if > > >>> return super(self.__class__,

using super

2010-01-18 Thread Jean-Michel Pichavant
class SubClass(Base): colour = "Red" def parrot(self): """docstring for Subclass""" return super(Subclass, self).parrot() I'm not a big fan of super, but I'm still wondering if >>> return super(self.__class__, self).parrot() would have made it. What if Subclass has mo

Re: using super

2008-01-09 Thread Basilisk96
On Jan 1, 12:11 am, Scott David Daniels <[EMAIL PROTECTED]> wrote: > Steven D'Aprano wrote: > > On Mon, 31 Dec 2007 16:19:11 -0800, Scott David Daniels wrote: > > >> Steven D'Aprano wrote: > >>> On Mon, 31 Dec 2007 08:03:22 -0800, Scott David Daniels wrote: > Steven D'Aprano wrote: ... > >

Re: using super

2008-01-09 Thread Basilisk96
On Jan 2, iu2 <[EMAIL PROTECTED]> wrote: > I missed new style classes though... Now I know how to use them (not > fully), but I must say it has been difficult. I'll appreciate some > good documentation about them. > > Thanks > iu2 This is a decent start: http://www.python.org/doc/newstyle/ Cheers

Re: using super

2008-01-02 Thread iu2
On Jan 1, 8:12 pm, Scott David Daniels <[EMAIL PROTECTED]> wrote: > We accept this seems natural to you. You don't seem to understand why > others might not think so. I fear this is the kind of thing that > separates programmers into two classes: the smart ones that can set up > the chains, and

Re: using super

2008-01-01 Thread Scott David Daniels
iu2 wrote: > On Jan 1, 12:32 pm, Steven D'Aprano <[EMAIL PROTECTED] > cybersource.com.au> wrote: >> import read_my_mind_and_do_what_I_want >> first. (Module expected in Python 9.7, due out in 2058.) > > That's because it seems to you like some esoteric feature. > To me it seems natural with OO. An

Re: using super

2008-01-01 Thread iu2
On Jan 1, 12:32 pm, Steven D'Aprano <[EMAIL PROTECTED] cybersource.com.au> wrote: > > import read_my_mind_and_do_what_I_want > > first. (Module expected in Python 9.7, due out in 2058.) That's because it seems to you like some esoteric feature. To me it seems natural with OO. > "Special cases ar

Re: using super

2008-01-01 Thread Steven D'Aprano
On Mon, 31 Dec 2007 22:56:41 -0800, iu2 wrote: > Indeed I might want to chain methods in all sort of ways: > @chain_call_before - call the parent's method before the derived method > @chain_call_after - call the parent's method after the derived method > @chain_call_sum - sum the result of the par

Re: using super

2008-01-01 Thread iu2
On Jan 1, 9:59 am, Michele Simionato <[EMAIL PROTECTED]> wrote: > No PEP, this would never pass. There would be no way > to stop a method from calling its parent: you would > lose control of your classes, so I think this is a > bad idea. Not all classes, only classes the programmer chooses to hav

Re: using super

2008-01-01 Thread Michele Simionato
On Jan 1, 7:56 am, iu2 <[EMAIL PROTECTED]> wrote: > no one has to remember to call the parent's get_name method. It's done > automatically. > > (A PEP maybe? I sense you don't really like it.. ;-) > What do you think? Good? Too implicit? > > iu2 No PEP, this would never pass. There would be no way

Re: using super

2007-12-31 Thread iu2
On Dec 31 2007, 6:03 pm, Scott David Daniels <[EMAIL PROTECTED]> wrote: > As to the original idea, better to give it up. > Typically code for a "chained" method "foo" that > returns a result will want to (in some way) use > the result from that call in forming its result. > Python's super allows yo

Re: using super

2007-12-31 Thread Scott David Daniels
Steven D'Aprano wrote: > On Mon, 31 Dec 2007 16:19:11 -0800, Scott David Daniels wrote: > >> Steven D'Aprano wrote: >>> On Mon, 31 Dec 2007 08:03:22 -0800, Scott David Daniels wrote: Steven D'Aprano wrote: ... > def chain(meth): # A decorator for calling super. > def f(self, *arg

Re: using super

2007-12-31 Thread Steven D'Aprano
On Mon, 31 Dec 2007 16:19:11 -0800, Scott David Daniels wrote: > Steven D'Aprano wrote: >> On Mon, 31 Dec 2007 08:03:22 -0800, Scott David Daniels wrote: >>> Steven D'Aprano wrote: ... def chain(meth): # A decorator for calling super. def f(self, *args, **kwargs): resul

Re: using super

2007-12-31 Thread Scott David Daniels
Steven D'Aprano wrote: > On Mon, 31 Dec 2007 08:03:22 -0800, Scott David Daniels wrote: >> Steven D'Aprano wrote: ... >>> def chain(meth): # A decorator for calling super. >>> def f(self, *args, **kwargs): >>> result = meth(self, *args, **kwargs) >>> S = super(self.__class__, s

Re: using super

2007-12-31 Thread Steven D'Aprano
On Mon, 31 Dec 2007 08:03:22 -0800, Scott David Daniels wrote: > Steven D'Aprano wrote: >> ... >> I'm not sure if this is your only problem or not, but super() only >> works with new-style classes, not with classic classes. You must >> inherit from object, or it cannot possibly work. >> >> Change

Re: using super

2007-12-31 Thread Gabriel Genellina
En Mon, 31 Dec 2007 12:08:43 -0200, Steven D'Aprano <[EMAIL PROTECTED]> escribi�: > On Mon, 31 Dec 2007 05:47:31 -0800, iu2 wrote: > >> I'm trying to make a method call automatically to its super using this >> syntax: > > > def chain(meth): # A decorator for calling super. > def f(self, *ar

Re: using super

2007-12-31 Thread Scott David Daniels
Steven D'Aprano wrote: > ... > I'm not sure if this is your only problem or not, but super() only works > with new-style classes, not with classic classes. You must inherit from > object, or it cannot possibly work. > > Change "class A" to "class A(object)". Absolutely correct. However, the sug

Re: using super

2007-12-31 Thread Steven D'Aprano
On Mon, 31 Dec 2007 05:47:31 -0800, iu2 wrote: > Hi > > I'm trying to make a method call automatically to its super using this > syntax: [snip code] I'm not sure if this is your only problem or not, but super() only works with new-style classes, not with classic classes. You must inherit from

using super

2007-12-31 Thread iu2
Hi I'm trying to make a method call automatically to its super using this syntax: class A: chained = ['pr'] def pr(self): print 'Hello from A' class B(A): def pr(self): print 'Hello from B' chain(B, A) b = B() b.pr() b.pr() will print Hello from B Hello from A I'm

Re: using super() to call two parent classes __init__() method

2007-08-17 Thread Evan Klitzke
On 8/16/07, 7stud <[EMAIL PROTECTED]> wrote: > When I run the following code and call super() in the Base class's > __init__ () method, only one Parent's __init__() method is called. As the other posters have mentioned, each class needs to make a call to super. This is because the super call does

Re: using super() to call two parent classes __init__() method

2007-08-16 Thread Steve Holden
7stud wrote: > When I run the following code and call super() in the Base class's > __init__ () method, only one Parent's __init__() method is called. > > > class Parent1(object): > def __init__(self): > print "Parent1 init called." > self.x = 10 > > class Parent2(object): >

Re: using super() to call two parent classes __init__() method

2007-08-16 Thread Alex Martelli
7stud <[EMAIL PROTECTED]> wrote: > When I run the following code and call super() in the Base class's > __init__ () method, only one Parent's __init__() method is called. > > > class Parent1(object): > def __init__(self): > print "Parent1 init called." > self.x = 10 > > cla

using super() to call two parent classes __init__() method

2007-08-16 Thread 7stud
When I run the following code and call super() in the Base class's __init__ () method, only one Parent's __init__() method is called. class Parent1(object): def __init__(self): print "Parent1 init called." self.x = 10 class Parent2(object): def __init__(self): pr

Re: Using super()

2006-07-19 Thread Michele Simionato
Michele Simionato ha scritto: > I believe the new style system was designed to allows this sort of > mixing and > that there are no issues at all. Thinking a bit more, there are no issues at all if you know what a new style class is and if you do not expect it to work as an old-style one ;) For th

Re: Using super()

2006-07-19 Thread Michele Simionato
Carl Banks ha scritto: > Pupeno wrote: > > I see, thank you. > > > > class MyConfig(ConfigParser, object): > > def add_section(self, section) > > super(MyConfig, self).add_section(section) > > > > seems to work and as expected. Is there anything wrong with it ? > > Wow. > > I highly r

Re: Using super()

2006-07-19 Thread Jan Niklas Fingerle
Pupeno <[EMAIL PROTECTED]> wrote: > class MyConfig(ConfigParser, object): > def add_section(self, section) > super(MyConfig, self).add_section(section) > > seems to work and as expected. Is there anything wrong with it ? yes. (1) There's a colon missing in the def-line. ;-) (2) Th

Re: Using super()

2006-07-19 Thread Carl Banks
Pupeno wrote: > I see, thank you. > > class MyConfig(ConfigParser, object): > def add_section(self, section) > super(MyConfig, self).add_section(section) > > seems to work and as expected. Is there anything wrong with it ? Wow. I highly recommend not doing this, unless the new type

Re: Using super()

2006-07-19 Thread Laszlo Nagy
> I see, thank you. > > class MyConfig(ConfigParser, object): > def add_section(self, section) > super(MyConfig, self).add_section(section) > > seems to work and as expected. Is there anything wrong with it ? > I have never seen this before. :) I don't know the answer, but I'm int

Re: Using super()

2006-07-19 Thread Pupeno
Laszlo Nagy wrote: > Pupeno írta: >> Hello, >> I have a class called MyConfig, it is based on Python's >> ConfigParser.ConfigParser. >> It implements add_section(self, section), which is also implemented on >> ConfigParser.ConfigParser, which I want to call. >> So, reducing the problem to the bare

Re: Using super()

2006-07-19 Thread Laszlo Nagy
Pupeno írta: > Hello, > I have a class called MyConfig, it is based on Python's > ConfigParser.ConfigParser. > It implements add_section(self, section), which is also implemented on > ConfigParser.ConfigParser, which I want to call. > So, reducing the problem to the bare minimum, the class (with a

Using super()

2006-07-19 Thread Pupeno
Hello, I have a class called MyConfig, it is based on Python's ConfigParser.ConfigParser. It implements add_section(self, section), which is also implemented on ConfigParser.ConfigParser, which I want to call. So, reducing the problem to the bare minimum, the class (with a useless add_section that