can i define a new method at runtime?

Jeff Epler jepler at unpythonic.net
Mon Jun 21 07:56:13 EDT 2004


Something like the untested code below will work, without the need to
"exec" anything.

def verifyInteger(x): ...
def verifyFloat(x): ...
verifiers = {"ControlName1": verifyInteger, "ControlName2": verifyFloat, ...}

class MyTextBox(TextBox):
    def __init__(self, controlname, ...):
        self.verifyfunc = verifiers[self.controlname]
        TextBox.__init__(self, ...) 
    ...
    def change(self):
        self.verifyfunc(self.value)

Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20040621/5ca225a3/attachment.sig>


More information about the Python-list mailing list