Basj
Basj

Reputation: 46423

Redefine an existing function

I define a subclass of PyControl like this :

class MyBitmapButton(wx.PyControl):
    def __init__(self, parent, id=-1, bmp=None, label='blah', pos = wx.DefaultPosition, size=(166,220), style = 0, validator = wx.DefaultValidator,
                 name = "mybitmapbutton"):
        style |= wx.BORDER_NONE 
        wx.PyControl.__init__(self, parent, id, pos, size, style, validator, name)
        self.myimg = wx.StaticBitmap(self, -1, bmp, pos=(8,8), size=(150,150))
        self.mytxt = wx.StaticText(self, -1, label, (6,165))

    def Bind(self, *args, **kwargs):
        self.Bind(*args, **kwargs)         # infinite recursion problem ! 
        self.myimg.Bind(*args, **kwargs)
        self.mytxt.Bind(*args, **kwargs)

I would like to override the standard Bind, but in this definition, I need to use the old Bind (that was provided by wx.PyControl).

With this current code, I get an infinite recusion loop problem :

How to reuse the old Bind in the definition of the new Bind ?

Upvotes: 2

Views: 128

Answers (2)

ndpu
ndpu

Reputation: 22571

Change this line self.Bind(*args, **kwargs) to:

super(MyBitmapButton, self).Bind(*args, **kwargs)

in python3 super will work without arguments:

super().Bind(*args, **kwargs)

from super docs:

Return a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class. The search order is same as that used by getattr() except that the type itself is skipped.
...

Upvotes: 1

jonrsharpe
jonrsharpe

Reputation: 122158

You need to use super here, to access the superclass's version of Bind:

super(MyBitmapButton, self).Bind(*args, **kwargs)

or, in Python 3, simply

super().Bind(*args, **kwargs).

Upvotes: 3

Related Questions