Override method name and original method access

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Nov 13 04:00:29 EST 2007


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. 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.

>> 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




More information about the Python-list mailing list