views:

42

answers:

1

Hello people,

I've got a small issue with derived classes, namely wx.ListItem with wx.ListCtrl. I succesfully derived wx.ListItem as a MediaItem, the code is not finished but you get the point:

class MediaItem(wx.ListItem):
    def __init__ (self, fullname):
        wx.ListItem.__init__(self)
        self.fullname = fullname
        self.filename = os.path.basename(fullname)
        # snap...

    def getFullname(self):
        return self.fullname

wx.ListCtrl gladly accepts that because of Pythons duck philosophy. But now the problem is that using the method wx.ListCtrl.GetItem(index) returns a ListItem, not MediaItem. Python complained about wx.ListItem not having an attribute getFullname.

Casting objects seems to be the wrong way to approach the solution. This probably has nothing to do with the problem, but I paste the offending line as is as well:

filename = self.filelist.GetItem(event.GetIndex()).getFullname()

Where self.filelist is a wx.ListCtrl.

A: 

I guess I should just suck it up, and regress to the suboptimal manual bookkeeping. When done tastefully, it's not a big deal but I had higher hopes for wxPython.

Supposedly (from what I searched and collected) the issue is with the proxy nature of wxPython class base. Were they written in pure Python, or I coded in C++, this would have worked well. But now the polymorphism of objects fails because of limitations in the design: the native C++ wx class won't get anything but a wx.ListItem and it will certainly only return a wx.ListItem back to wxPython.

My "solution" thus is to derive wx.ListCtrl instead of wx.ListItem, store needed information and control the appearances there.

progo