How do I say "Is this a function"?

"Martin v. Löwis" martin at v.loewis.de
Sat Apr 26 21:08:58 EDT 2008


> def f1():
>    print "In f1"
> 
> def f3():
>    print "In f3"
> 
> def others():
>    print "In others"
> 
> for i in xrange(1,3):
>    fct = "f%d()"%(i+1)
>    try:
>       exec fct
>    except:
>       others()

I'd write that as

for i in xrange(1,3):
    globals().get("f%d" % (i+1), others)()

Regards,
Martin



More information about the Python-list mailing list