Comment on Functional Programming in PythonparentComments−jennasys6ylambda use in Python is discouraged in general. If you need something beyond an expression, PEP-8 encourages you to use a def in that case instead.−NoahTheDuke6yThe issue with that is the lack of lexical scope. Lambdas have lexical scoping, def functions do not. To “fake” lexical scope, you have to use the nonlocal or global statements, which are gross and self-defeating.−throwaway_pdp096yNot sure I understand, defs have lexical scoping too, if you just read variables (or am I getting rusty?). x = 3 def prt(): print(x) prt() prints 3, as expected.It's when you assign that you need P3's global etc. annotations, and you can't assign in lambdas.
Comments
lambda use in Python is discouraged in general. If you need something beyond an expression, PEP-8 encourages you to use a def in that case instead.
The issue with that is the lack of lexical scope. Lambdas have lexical scoping, def functions do not. To “fake” lexical scope, you have to use the nonlocal or global statements, which are gross and self-defeating.
Not sure I understand, defs have lexical scoping too, if you just read variables (or am I getting rusty?).
prints 3, as expected.It's when you assign that you need P3's global etc. annotations, and you can't assign in lambdas.