On Mon, 21 Feb 2022 at 09:38, Eryk Sun <eryk...@gmail.com> wrote: > > On 2/20/22, Abdur-Rahmaan Janhangeer <arj.pyt...@gmail.com> wrote: > > > > Out of curiosity, why doesn't Python accept > > def (): > > return '---' > > The `def` keyword is compiled as an assignment statement, not an > expression that evaluates anonymously on the stack. Using `def` as an > expression would require new syntax. For example, maybe something like > the following: > > funcs[i] = def (x, *, y=0) := ( > if x < y: > return spam(x) > return eggs(x) > ) >
There's always decorators. >>> @functools.partial(operator.setitem, funcs, i) ... def _(x, *, y=0): ... if x < y: ... return spam(x) ... return eggs(x) ... >>> funcs[i] <function _ at 0x7f6fd4b36b90> (Has the side effect of setting _ to None.) ChrisA -- https://mail.python.org/mailman/listinfo/python-list