4 ms·
> While i normally prefer indentation to demark function scope, it has one huge drawback: When writing callbacks, especially chained promises, Python has no way
by dragonwriter 5y ago
> While i normally prefer indentation to demark function scope, it has one huge drawback: When writing callbacks, especially chained promises, Python has no way to express the following in a convenient way.
foo().then(f => {
return bar(f)
}).then(b => {
print(b)
})
Huh? Assuming an identical API structure, it's syntax for exactly that would be:
foo().then(bar).then(print)
> Lambdas only allow a single expression.
Sure, but your problem code uses only single-expression lambdas with superfluous blocks (where, in fact, the lambdas are also superfluous), so its literally the worst possible thing to say that Python can't express as cleanly.
Also, single-expression lambdas can handle a lot, because expressions are easy to combine to arbitrary levels, it's only multistatement imperative blocks you can't do in a lambda, but Python has expression forms that cover lots of uses of imperative blocks already.
- Too 5y agoSorry i only used a single expression in the example for brevity. Normally they contain a lot more than one. I thought that would be obvious from the context, the form you presented would fall into the “define function upfront” category.