Override method name and original method access

Chris Mellon arkanes at gmail.com
Tue Nov 13 15:14:07 EST 2007


On Nov 13, 2007 3:00 AM, Gabriel Genellina <gagsl-py2 at yahoo.com.ar> wrote:
> En Tue, 13 Nov 2007 01:45:31 -0300, Donn Ingle <donn.ingle at gmail.com>
> escribió:
>
> >> You need to be a new-style class (that is, you must inherit from
> >> object) for super() to work.
> > Problem is that my classes inherit already, from others I wrote. So,
> > should
> > I explicitly put (object) into the ones at the top?
>
> Changing from old-style to new-style classes may have some unintended side
> effects.

Extremely unlikely, though. It's very doubtful that the OP is doing
anything that requires old-style classes.

>You may prefer keeping your old-style classes and avoid using
> super, if you don't have multiple inheritance. Just call explicitely the
> base class.
>

It's a lot harder to change your object hierarchy to use super() after
the fact than it is to do explicit superclass calling in some class
that you decide to use different signatures for. The use of super()
(and new style classes) should be the default implementation, and you
should only move away from that with an explicit reason. Telling
people not to use super when there's no reason not to doesn't help
anything.

> >> Other than that, you are using it
> >> correctly here.
> > Well, even with <One.add(stuff)> it seems to be calling to local
> > overridden
> > method. In a bit of a hurry, so can't test again right now.
>
> Remember that you must pass `self` explicitely. That is:
>
> class One:
>   def __init__(self):
>    self.stuff = []
>   def add (self, stuff):
>    self.stuff.append(stuff)
>
> class Two(One):
>   def __init__(self, otherstuff):
>    One.__init__(self)
>    One.add(self, otherstuff)
>    self.unrelated = []
>   def add (self, data):
>    self.unrelated.append(data)
>
> py> x = One()
> py> x.add(1)
> py> x.stuff
> [1]
> py> y = Two(2)
> py> y.add(3)
> py> y.stuff
> [2]
> py> y.unrelated
> [3]
>
> --
> Gabriel Genellina
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list