Problem
I have code like this
if condition:
a = f(x)
else:
a = g(y)
Initialization ainside a block looks bad to me. Could it be better written?
I cannot use the ternary operator because function names and / or argument lists are long. When I say long, I mean the following expression
a = f(x) if condition else g(y)
will take more than 79 (sometimes even more 119) to the names of characters instead of a, f, g, x, yand condition. Using multiple slashes will make the code ugly and messy.
I do not want to initialize awith the result of one of the defaul functions, because both functions are slow, and I cannot allow such utility
a = g(y)
if condition:
a = f(x)
None, ?
a = None
if condition:
a = f(x)
else:
a = g(y)
: C ++ . ES6 let — , C ++. , var , , Python.
, , .
Update
for obj in gen:
if predicate(obj):
try:
result = f(obj)
except Exception as e:
log(e)
continue
else:
result = g(obj)
else:
result = h(obj)
display(result)
gen, result .
- result .
pythonic, result?
?
if/else/for/ .. Python?