Is it possible to "re-use" the value of a parameter of an if-clause?
I have a function that returns either True or a dictionary:
def foo():
if random.randint(0, 1) == 0:
return True
else:
return {"time": time.time()}
If foo() returns the dictionary, I also want to return it. If foo() returns True I want to just continue.
Currently I'm using:
if_value = foo()
if if_value is not True:
return if_value
My goal would be to avoid saving the return value of the function into a variable, because it makes my code really ugly (I need to do this about 20 times in a row).
When using a Python shell, it seems to work like this:
if function_that_returns_True_or_an_int() is not True:
return _
Any suggestions about this?
returnthere but no function. Please provide a minimal reproducible example so it is easy to understand the scenario. For example, you might be able to do something likereturn func() or some_value, but again - it is hard to say without a proper minimal reproducible examplefooitself was less important, more important is what you do with it which is currently not clear. You doreturn valuein case the value is not true, but what is theelse? What is the function thisreturnbelongs to? Knowing this will allow to offer other solutions apart from using the walrus (maybe ones that remove the need of a condition altogether)