Rectangle 27 0

python objects not being passed to function in flask route?


testfun(aaa)
  • Use keyword arguments: If a variable number of arguments are needed and testfun cannot be broken up you can use keyword arguments to simplify the calling of the function: def testfun(arg1=None, arg2=None, arg3=None, arg4=None, arg5=None, arg6=None, arg7=None, arg8=None): # do stuff with args # call it as so args = {"arg1": "some args", "arg5": "have defaults"} if complex_condition: args["arg3"] = 17 elif another_condition: args["arg7"] = 10 testfun(**args)
  • Use multiple functions: If testfun is doing a lot of work then break it up into multiple functions that return intermediate transformations of the data: def start_test(arg1, arg2, arg3): # do stuff with args return result_stage_1 def continue_test(arg3, arg4, arg5): # do stuff with args return result_stage_2 def finalize_test(arg7, arg8): # do stuff with args return real_result

Encapsulate state and behavior with classes (or closures): If neither of the above will work for you then you probably need to leave the realm of stateless functions and create classes or closure-creating functions to preserve your state and enable you to modify behavior as needed.

If testfun requires too many params there are several ways you can DRY up the code:

This is due to Python's scoping rules (as @johnthexiii pointed out) - testfun->aaa is bound to the global scope because there is no variable named aaa declared anywhere inside of testfun and there is no enclosing scope (i. e. testfun is not declared inside another function or class).

You'll want to pass aaa as an argument to testfun:

Note