Python dynamic function selection

Tuure Laurinolli tuure at laurinolli.net
Wed May 12 08:31:50 EDT 2004


Eric wrote:
>>>>def func(): 
> 	print "true"

>>>>rules=(func,)
>>>>for rule in rules: 
>         rule

> I expect the final function to print true, but instead i have
> <function func at 0x00DC6EB0>

Why? You are not calling the function, so in the interactive interpreter 
you get the representation instead.

>>>>for rule in rules:
> 
>         rule("true")

Call it without passing arguments. The parentheses denote that the name 
to their left is callable and should be called with whatever arguments 
there are inside them, there can also be 0 arguments.

for rule in rules:
   rule()



More information about the Python-list mailing list