Ugly modification of a class, can it be done better ?

Stef Mientki stef.mientki at gmail.com
Thu May 20 20:13:30 EDT 2010


hello,

This might be a strange question, but as a practical guy, I'm not
searching for the best solution, but for a practical solution.

I've a class which I've used very extensively.
Now I want to extend that class with an action on a double click event,
but that action is determined by the main program.
Normally I would create an derived class and add that specific action.
But I find it too much effort in this specific case, half of the
instances need the extension, half of it dont.
So I want to change the behavior of the class dynamically.
I've done it by adding a global variable (Base_Grid_Double_Click) in the
module,
initial set to None,
but can be changed by the main program to some callback function.
(see the code below)
Is this a valid construction ( sorry I'm not a programmer),
or are there better ways to accomplish similar dynamic behavior ?

thanks,
Stef Mientki



#== module grid_library ==

Base_Grid_Double_Click = None

class Base_Grid ( wx.grid.Grid ) :
  def __init__ ( self, ....

    self.Bind ( gridlib.EVT_GRID_CELL_LEFT_DCLICK , self._On_Double_Click )
   

  def _On_Double_Click ( self, event ) :
    if Base_Grid_Double_Click :
      Row = event.Row
      Col = event.Col
      Col_Label = self.GetColLabelValue ( Col )
      Row_Label = self.GetRowLabelValue ( Row )
      Base_Grid_Double_Click ( self.GetCellValue ( Row, Col ),
                               Col_Label, Row_Label )
    else :
      event.Skip ()

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100521/596f8f75/attachment.html>


More information about the Python-list mailing list