can i define a new method at runtime?
Larry Bates
lbates at swamisoft.com
Sun Jun 20 09:14:10 EDT 2004
You are close. Try something like
def verifyInteger(x):
try:
string.atoi(x.value)
except ValueError :
message(x," needs to be an integer")
x.setFocus()
In a dictionary put keys (controlNames) and
pointers to what function (or method) to call
for that controlName.
verifiers={'controlName1': verifyInteger,
'controlName2': verifyFloat,
...
'controlNameN': veryfySomething}
In your program call the function from the
dictionary as follows:
verifiers['controlName1'](x)
verifiers['controlName2'](x)
...
This works because the result of the getting
verifiers[controlName] is a pointer to a
function instead of a value. This pointer
has a call method, so you can just put the
arguments after it.
HTH,
Larry Bates
Syscon, Inc.
"Raoul" <raoulsam at yahoo.com> wrote in message
news:7b22ae5b.0406181049.479d1a61 at posting.google.com...
> 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"],["controlNam
e3
> "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