Polymorphing - how?

Chris Liechti cliechti at gmx.net
Wed May 1 17:05:28 EDT 2002


Jaros³aw Zabie³³o <webmaster at apologetyka.com.pl (delete .PL)> wrote in 
news:hjk0duk7aj6bcmi3tkdqnasui6bl593j8t at 4ax.com:

> I would like to execute functions which names will be changed during
> runtime. The following approach does not work. :-(
> 
> def First(s):
>      print "myFun1: %s" % s
> def Second(s)
>      print "myFun2: %s" % s
> name = 'First'
> name('blah') # It doesn't work. :-(
>                 # TypeError: 'str' object is not callable

when you leave away the quotes it works... yeah i know it's not likely that 
this solves your real problem ;-)

the "getattr()" builtin is your friend when the functions are in a class or 
"globals()['name']" for globaly defined functions.

chris
-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list