python - leave optional parameters out conditionally -
consider function:
def foo(x, y=1): return x*x + y*y now want call foo this:
if param_y == none: z = foo(3) else: z=foo(3,param_y) where param_y determined somewhere before. want simplify this, such there 1 foo-call in code, since if have multiple of such optional parameters, i'd enormous (and ugly!!) if-else clause, can do:
z = foo(3, param_y if param_y != none else 1) but requires caller know 1 default value of y, find bit ugly well. there alternative syntax this, like:
z = foo(3, if param_y != none: param_y)
can't update foo function?
def foo(x, y): if y none: y = 1 return x*x + y*y and call everytime :
z=foo(3,param_y)
Comments
Post a Comment