wxpython and inheritance

This question comes in two parts, and is related to how wxPython interacts with the python object model.

Intuitively this should be no problem, based on past usage of python and understanding of object oriented programming

class MyRandomBaseClass(object):
  def __init__(self): pass

class MyRandomFrame(wx.Frame,MyRandomBaseClass):
  def __init__(self,parent,id,**kwargs):
    wx.Frame.__init__(self,parent,id,**kwargs):
    MyRandomBaseClass.__init__(self)

But, code using this design pattern fails to run. There is a TypeError in the wxpython code because MyRandomFrame inherits something else. It expects _wxEvtHandler_p and claims to not be getting it--even though it is.

This base class can be empty (or object) and the problem doesn't go away.

I attempted to use python's new object model to add a base class at runtime via __bases__, but it wasn't defined, suggesting that wxPython classes are "classic classes." Is this the case? If it is, is there a known workaround for this issue? I haven't been able to try this out in 2.5 yet (still using 2.4.1.2) so perhaps there's a fix there, but the changelog doesn't seem to mention it. (I have no trouble upgrading, I'd just rather deal with the problem in a more correct way first).

I'd imagine that this is a fairly common thing, but perhaps what I'm doing goes against basic wxWidgets application design. If so, Is there a way that I can accomplish a similar effect?

Basically, I need a custom base class for most, if not all, objects in my software because the backend has its own event system and all of the objects need to 'plug into' it, most importantly, by registering themselves with a few manager/dispatcher objects. Multiple base classes really seems like the 'correct' object oriented approach, but i'm open to new ideas. I can't use the wx event system because it is only one of many user interfaces that could be/have been ateached to this code.

thanks,

Brian

Brian L writes:

class MyRandomBaseClass(object):
        def __init__(self): pass

class MyRandomFrame(wx.Frame,MyRandomBaseClass):
        def __init__(self,parent,id,**kwargs):
                wx.Frame.__init__(self,parent,id,**kwargs):
                MyRandomBaseClass.__init__(self)

What you are talking about is making 'mixin' classes.
MyRandomBaseClass is a mixin, and yes it can be done - I do it
all the time to provide common functionality.

I bet you are using wxPython 2.4.2.4 or earlier, in which case
you need to not use new-style Python classes in your mixin:

class MyRandomBaseClass(object):
        def __init__(self): pass

If you are using wxPython 2.5 or higher, I see no reason why
your code as posted wouldn't run.

···

--
Paul

Paul McNett writes:

I bet you are using wxPython 2.4.2.4 or earlier, in which
case you need to not use new-style Python classes in your
mixin:

class MyRandomBaseClass(object):
        def __init__(self): pass

Let me see if I can get this right this time. :slight_smile:

It needs to be old-style, and not inherit from a new-style type:

class MyRandomBaseClass:
        def __init__(self): pass

···

--
Paul

Brian L wrote:

But, code using this design pattern fails to run. There is a TypeError in the wxpython code because MyRandomFrame inherits something else. It expects _wxEvtHandler_p and claims to not be getting it--even though it is.

The way I was using the old SWIG is incompatible with new-style classes, so you can only do mixins with 2.4 using classic classes.

known workaround for this issue? I haven't been able to try this out in 2.5 yet (still using 2.4.1.2) so perhaps there's a fix there, but the changelog doesn't seem to mention it.

See the first item here:

http://wxpython.org/MigrationGuide.html#swig-1-3

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Thank you for the incredibly fast responses. Glad to know it's been improved in 2.5 (upgrading now).

Brian

···

On Apr 26, 2004, at 7:20 PM, Robin Dunn wrote:

Brian L wrote:

But, code using this design pattern fails to run. There is a TypeError in the wxpython code because MyRandomFrame inherits something else. It expects _wxEvtHandler_p and claims to not be getting it--even though it is.

The way I was using the old SWIG is incompatible with new-style classes, so you can only do mixins with 2.4 using classic classes.

known workaround for this issue? I haven't been able to try this out in 2.5 yet (still using 2.4.1.2) so perhaps there's a fix there, but the changelog doesn't seem to mention it.

See the first item here:

http://wxpython.org/MigrationGuide.html#swig-1-3

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org