can i define a new method at runtime?

Raoul raoulsam at yahoo.com
Fri Jun 18 14:49:13 EDT 2004


I have a GUI application where I want to assign validation methods to
controls.

If my control myTextBox has a change() event for on change and I want
to make it verify the input is an integer I could do...

def myTextBox.change():
   verifyInteger(myTextBox.Value)

def verifyInteger(x):
   try:
      string.atoi(x.value)
   except ValueError :
      message(x," needs to be an integer")
      x.setFocus()

but i have literally hundreds of these things to do....

I'd like to be able to say somethign like

myTextBox.change = lambda x : verifyInteger(x)

so when i initialize my form i'd like to run through a list that looks
like

[["controlName1","verifyInteger"],["controlName2,"verifyFloat"],["controlName3
"verifyInteger"]

but i can't seem to make this work.

I've tried to use exec("def foo = lambda x: do something....")

but that doesn't seem to work....

Got any ideas ???



More information about the Python-list mailing list