Usually you should follow the advice of San4ez and just use the temporary variable here. I continue to present a few methods that may be useful in certain circumstances:
In general, if you want to associate a name only for subexpression (usually for what you need a temporary variable), you can use lambda:
x = (lambda result=some_very_complex_computation(y): [(t, result) for t in z])()
:
x = zip(z, itertools.repeat(some_very_complex_computation(y)))
,
, Python, , some_very_complex_computation , . , , Haskell, .
"" : Memoization
some_very_complex_computation :
from functools import lru_cache
@lru_cache()
def some_very_complex_computation(y):
Python 3. Python 2 :
from functools import wraps
def memoize(f):
cache = {}
@wraps(f)
def memoized(*args):
if args in cache:
return cache[args]
res = cache[args] = f(*args)
return res
return memoized
@memoize
some_very_complex_computation(x):