python - Pass some random value returned from a function to another function -
i stuck in situation need pass value (which random/different) returned function function, , sequence in functions called undefined figured @ run-time based on user inputs.
for example,
def func1(some_value): # use some_value whatever purpose # code return some_random_value def func2(some_value): # use some_value whatever purpose # code return some_random_value def func3(some_value): # use some_value whatever purpose # code return some_random_value
so let's assume if func2
called first, initial/default value passed parameter some_value
, function return some_random_value
. now, don't know function called next, whatever function called some_random_value
returned previous function (in case func2
) should passed parameter some_value
next called function (let func1
). , process goes on , on.
what recommended way achieve this? should done using global variable value amended each time function runs store function's return value? if yes, how?
more specifically
a cli allow user choose action , appropriate function called according action. last returned value function should in memory till application ends. after function performs it's task, it'll return value. value required when other function called using cli action. again, next function process data using last function's return value, , return processed value, later used next function or cli action.
i thinking instead of returning value of functions, create global variable default value:
common_data = 'some string'
and in every function definition, add:
global common_data common_data = 'new processed string'
this ensure next function call passed value last saved in common_data previous function.
but seems non-recommend solution, @ least think so.
please allow me edit or elaborate question if unable explain situation properly.
thank you
i deliver on recursion error. ^^
from random import choice random import randint def get_fs(f): return [x x in (func1, func2, func3) if x != f] def func1(some_value, fs): # use some_value whatever purpose # code f = choice(fs) print("func1", f.__name__) return f(randint(1,10), get_fs(func1)) def func2(some_value, fs): # use some_value whatever purpose # code f = choice(fs) print("func2", f.__name__) return f(randint(1,10), get_fs(func2)) def func3(some_value, fs): # use some_value whatever purpose # code f = choice(fs) print("func3", f.__name__) return f(randint(1,10), get_fs(func3)) def main(): functions = [func2, func3] func1(randint(1,10), functions) if __name__ == '__main__': main()
Comments
Post a Comment