wxPython/Python - overriding strangeness

Hi,

I am getting different results based on how I override the OnPrintPage
method (for wxPrintout.)

Below are two samples with only the essentials. They try to draw a circle
on
the page. The first one works fine - where the subclass redefines the
OnPrintPage. The next one which reassigns the function fails

This version - where the subclass redefines the OnPrintPage function works
fine.

#!/usr/bin/env python

from wxPython.wx import *

class MyPrintout (wxPrintout):
   def __init__ (self, func): wxPrintout.__init__ (self); self.func = func
   def OnPrintPage (self, n): return self.func (self, n)

def OnPrintPage (PrintOutObj, n):
   dc = PrintOutObj.GetDC()
   dc.BeginDrawing ()
   dc.DrawCircle (100,100,100)
   dc.EndDrawing ()
   return true

app = wxPySimpleApp ()
frm = wxFrame (None, -1, "Main Frame")
frm.Show ()

prn = wxPrinter ()
po = MyPrintout (OnPrintPage)
prn.Print (None, po, true)

app.MainLoop ()

···

#####################
This is the second file
#####################

#!/usr/bin/env python

from wxPython.wx import *

class MyPrintout (wxPrintout):
   def __init__ (self, func): wxPrintout.__init__ (self); self.func = func
   def OnPrintPage (self, n): return self.func (self, n)

def OnPrintPage (PrintOutObj, n):
   dc = PrintOutObj.GetDC()
   dc.BeginDrawing ()
   dc.DrawCircle (100,100,100)
   dc.EndDrawing ()
   return true

app = wxPySimpleApp ()
frm = wxFrame (None, -1, "Main Frame")
frm.Show ()

prn = wxPrinter ()
po = MyPrintout (OnPrintPage)
prn.Print (None, po, true)

app.MainLoop ()

#########

The 2nd version does not display anything on the page.

Even if I just add this line

po.OnPrintPage = lambda n: OnPrintPage (n)

to the first version just after "po = MyPrintout (OnPrintPage)" - i get a
blank page.

My examples using just python to get a difference in calling semantics for
subclassing functions vs. reassigning functions do not seem to bring out
the
difference.

Could someone throw some light on this?

Thanks,
Chirayu.

Email: thephoenix235@gmx.net
Web: http://cs.oregonstate.edu/~krishnch/ms.html

--
+++ GMX - Mail, Messaging & more http://www.gmx.net +++
Bitte lächeln! Fotogalerie online mit GMX ohne eigene Homepage!

Chirayu Krishnappa wrote:

My examples using just python to get a difference in calling semantics for
subclassing functions vs. reassigning functions do not seem to bring out
the
difference.

Could someone throw some light on this?

If I understand your samples correctly, you are running into the difference between bound method objects and simply assigning a function object to be an attribute of a class. For example:

  >>> class X:
  ... def foo(self):
  ... pass
  ...
  >>>
  >>> def bar(self):
  ... pass
  ...
  >>>
  >>> x = X()
  >>> x.xbar = bar
  >>>
  >>> x.foo
  <bound method X.foo of <__main__.X instance at 0x87ff9b4>>
  >>> x.xbar
  <function bar at 0x8803bac>
  >>>

The code in wxPython that supports overriding virtual C++ methods in Python does an explicit lookup in the instance for a bound method object of the desired name in order to find the callable object to invoke. When you just assign a function to an instance attribute you don't get a bound method and so the lookup fails. wxPython needs real method objects for overridden methods.

···

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