dimanche 29 juillet 2018

python To efficiently use the result of function in if statement

Is there any other code form, that one can both use a function in if statement and get the value of function without executing the function twice?

For example,

There exists a function, fun1(arg), which takes an hour to return its result (The result value can be either None or some int)

and I want to do some further calculation(for example get its squared value) only if the result from fun1 is not None.

This will be done by:

result = fun1(arg) 
if result: 
    result = result * result

Is there any shorter form such as

if (result = fun1(arg)):
    result = result * result

in python?

Aucun commentaire:

Enregistrer un commentaire