Morning all,
Can someone advise, is it possible to achieve something like this:
def myFunction(someParam)
if someParam == 1:
return "This and that"
else:
return None
someVariable = someVariable || myFunction(valuePassedIn)
ie, given "someVariable" has an existing value, I want to preserve that value, or replace it with the value returned from "myFunction", provided that is not None?
Obviously, I can do something like:
if myFunction(valuePassedIn) != None:
someVariable = myFunction(valuePassedIn)
But this of course is calling myFunction twice. Alternatively, I could do:
myFuncRes = myFunction(valuePassedIn)
if MyFuncRes != None:
someVariable = myFuncRes
But it'd be good if I could reduce this to a single line of code, as per my "||" line in the first code snippet above. Obviously I've seen the conditional expressions documentation, but unless I'm misunderstanding, this would still involve calling myFunction twice?
I guess one other option would be to pass someVariable in as an additional param to myFunction and use this as the "else" return instead of None, but I can't help feeling this is kinda messy - and Python probably has something more slick I could use?
Thanks
if someParam = 1in yourmyFunctionis actually doing an assignment, not a comparison.SyntaxError