3 ms·
> lambdas only permitting a single expression Use a tuple, maybe walrus, and return the last item[-1].
by mixmastamyk 1y ago
> lambdas only permitting a single expression
Use a tuple, maybe walrus, and return the last item[-1].
- dapperdrake 1y agoThat idea sounds good. How do I get variables for not redoing long-running computations that depend on one-another? So, what if the third tuple value depends on the second and the second in turn depends on the first?
- int_19h 1y agoYou can abuse list and sequence comprehensions for this. `for..in` is effectively a variable binding since you can target a freshly created list or a tuple if you need to bind a single value. So: [x for x in [some_complicated_expression] if x > 0 for y in [x + 1] ... ][0] That said, I wouldn't recommend this because of poor readability.
- mixmastamyk 1y agoThat’s what walrus is for: future = lambda age: ( print('Your age is:', age), older := age + 5, print('Your age in the future:', older), older, )[-1] print(future(20)) # out Your age is: 20 Your age in the future: 25 25