I have a json sample getting it from a function, and have to pass some of it's parameters to my function but if they exist.
def myfunction(var1=None):
some code
return
if myjson['parameter1'] == 'somestring':
var1 = myjson['parameter1']['sub1']j
myfunction(var1=var1)
If the condition is not true, var1 will be undefined and leads to unbound local error. So what do I have to do? Is there any way to check existance in function input?
if 'var1' not in globals():
var1 = None
I know I can use ablve code to assign var1. But I want to know if there is any better design or pythonic way pattern to do this.
I tried
myfunction(var1=var1 if 'var1' in globals())
but doesn't work.
var1
should always exist, but may have aNone
value. So in this case, you can always passvar1
, as aNone
value is also the default parameter value.