tags:

views:

90

answers:

5

This seems very verbose, particularly with long function names, is there a better way to do this in Python?

if someRandomFunction():
    variable = someRandomFunction()

Edit: For more context variable is not already defined, and it will be a new node on a tree. I only want to create this node if someRandomFunction() returns a value. And someRandomFunction() is supposed to return the string representation of some node from a different type of tree.

+8  A: 

Could you:

variable = someRandomFunction() or variable

See Boolean Operations in the Python documentation for more information.

Greg Hewgill
That's the solution I like. I think it reads pretty well.
kindall
It's not clear from the question as stated whether `variable` is defined yet before this line of code gets executed. If `someRandomFunction()` evaluates to False, this raises a NameError exception because variable isn't defined yet.
ma3
@ma3204: I think we have to assume it's already defined, because if it's not and `someRandomFunction` returns false, then any code that used `variable` afterwards would also result in a `NameError` in the original code.
Adam Rosenfield
I don't see how we can assume it has been defined from the question as stated-- that is exactly my point and (I believe) the same point S. Lott has been trying to make. Nor can we assume that the variable needs to be defined after this point in the code if the function returns a False value. S. Lott's answer most accurately reflects the answer to the question asked.
ma3
In my particular case variable doesn't exist before hand and it will simply be checked if the node exists on a tree.
Colin Barnes
+5  A: 
temp= someRandomFunction()
if temp:
    variable = temp
S.Lott
A: 

(Apparently you can't delete your answers if you haven't registered.)

These are not the droids you're looking for... move along...

Vicki Laidler
I'm not the mysterious downvoter but this doesn't address the problem. It will set `variable` to `someRandomFunction()` for any value regardless of if it's truthy or not. Also, unless `someRandomFunction` is buggy (in which case it should be fixed, wrapped or replaced) there is little (read 'absolutely no') reason to expect a `NameError` to be thrown here.
aaronasterling
Ah! I misread the question as "set variable to result of function if _function_ exists", thus the NameError. Thanks for your comment. I'll remove this answer shortly.
Vicki Laidler
A: 

A bit unorthodox perhaps, but you could modify someRandomFunction() so that it saves its last result in a function attribute before returning it, you could then do this.

def someRandomFunction():
    ...
    someRandomFunction.result = <...>
    return someRandomFunction.result

if someRandomFunction():
    variable = someRandomFunction.result
martineau
A: 

I don't think your answer is too verbose. It says exactly what it does. However, since you've already said it's too verbose for your tastes I would opt for the

s = myFunc() or someVariable

approach

jaydel