0

Working in Python 3.8.

I prefer to have one return per function as it makes debugging easier.

Current code which I can live with:

if foo:
   return redirect(url_for('what.what'))
else:
   return render_template('who.who', form=my_form)

Desired code though:

if foo:
   ret_func = redirect
   ref_func_args = url_for("what.what")
else:
   ret_func = render_template
   ret_func_args = ('who.html', form=my_form)   # syntax help needed here

return ret_func(ret_func_args)                  # and probably some syntax help here also

Python understandably doesn't like the ret_func_args = ('who.html', form=my_form) and in particular form=my_form.

render_template is expecting a keyword argument named 'form'.

What is the proper syntax for ret_func_args = ('who.html', form=my_form) and perhaps the ret_func(ret_func_args) line? The ret_func(ret_func_args) does work if foo is true but can not figure out how to pass named parameters when foo is false in this case.

Tom
  • 1
  • You left out `render_template` in `ret_func_args = ('who.html', form=my_form)`, shouldn't it be `ret_func_args = render_template('who.html', form=my_form)`? You can't just arbitrarily set args like what you are doing and expect the "right" deferencing to the intended values to happen (it appears you are trying to destructure your program in a completely unsupported way), just do what you were doing in the first example. – metatoaster Jun 23 '21 at 05:10
  • 1
    If you're that into one-return-per-function, the typical approach would be to do `retval = whatever(blah, blah, blah)` instead of `return whatever(blah, blah, blah)`, and then `return retval` at the end. – user2357112 Jun 23 '21 at 05:14
  • 1
    Also, even if that thing you were trying worked, it would make things *harder* to debug, not easier. – user2357112 Jun 23 '21 at 05:15
  • 1
    Alternatively, you will need to separate out [normal arguments from keyword arguments](https://stackoverflow.com/questions/1419046/normal-arguments-vs-keyword-arguments), and invoke the result as appropriately, e.g. `args = (url_for('what.what'),`, and `args = ('who.html',)`, `kwargs = dict(form=my_form)`), and invoke `ret_func(*args, *kwargs)`. – metatoaster Jun 23 '21 at 05:15

0 Answers0