In Python how do I run an array of functions -


here problem have number of functions defined , want loop through list of these functions , run them 1 @ time in correct order.

def one():     print "one "  def two():     print "two "  def three(): "three "     print "three "  arr = ('one','two','three')  fnc in arr:     <some how run function name in variable fnc> 

any appreciated, beginner python , django.

python functions first order objects; put them in sequence:

arr = (one, two, three)  fnc in arr:     fnc() 

you store strings too, need turn function object first. that'd busywork don't need do.

you can still turn strings objects; globals() function gives current global namespace dictionary, globals()['one'] gives object referenced name one, give access every global in module; if made mistake lead hard track bugs or security holes (as end-users potentially abuse functions didn't intent called).

if need map names functions, because, say, need take input else produces strings, use predefined dictionary:

functions = {     'one': one,     'two': two,     'three': three, } 

and map string function:

function_to_call = 'one' functions[function_to_call]() 

your function names not need match string values here. using dedicated dictionary limit can called.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -