const f = (x, y) => {
const z = x + y;
const w = z * 2;
return z - w + x;
};
In Python, you cannot do this:
f = (
lambda x, y:
z = x + y;
w = z * 2;
return z - w + x;
)
Instead, you need to pull it out into a def:
def f(x, y):
z = x + y;
w = z * 2;
return z - w + x;
Sometimes, this is no big deal. But other times, it's damn annoying; the language forces you to lay out your code in a less intuitive way for no obvious benefit.
Actually you can. If you really want a multi-line lambda with your example...
```f = lambda x, y: [
z := x + y,
w := z 2,
z - w + x,
][-1]```
* That version does look strange, as it uses a list in order to get that last calculation. But I often use lambdas to check results in parametrized tests, and they naturally spread to multiple lines without the list hack since they're chains of comparisons.
greener_grass|1 year ago
skeledrew|1 year ago
```f = lambda x, y: [ z := x + y, w := z 2, z - w + x, ][-1]```
* That version does look strange, as it uses a list in order to get that last calculation. But I often use lambdas to check results in parametrized tests, and they naturally spread to multiple lines without the list hack since they're chains of comparisons.
vishnudeva|1 year ago