I am a relative newby, but Python has finally stuck. Several previous attempts over the decades had me recoiling in horror at significant whitespace, but this time I found it to be tolerable since most text editors now work around its problems in a fairly intuitive and transparent way!
But, coming mainly from Lua, the claim of first class functions in Python still seems a stretch. If I am treading a well worn path here, please go easy on me and point me specifically to where the discussion is at!
- Why the âlambdaâ keyword?
myfunc = lambda x: x**2
myfunc = def(x): return x**2
The hypothetical second version deprecates a keyword and makes the definition of a lambda more consistent with the definition of an ordinary function.
- Remove the single-line restriction on lambdas.
myfunc = def(x):
return x**2
This syntax easily extends to multi-line function definitions allowing removal of the single-line restriction on lambdas.
- Make this the ordinary way of defining functions.
def myfunc(x):
return x**2
The traditional form of function definition becomes syntax sugar for the proposed new form in 2 (as in Lua).
- Now functions can be truly first-class!
myfunclist = [
def(x): return x**2,
def(x, y):
return x * y
]
We can define single-line or multi-line functions anywhere a reference to a function object would be valid without having to separately define them and invent a redundant dummy name. This also makes the syntax consistent with class definitions.
This proposal seems sufficiently obvious that it must have been thought of before. I am really interested to learn by understanding why it has been rejected (or at least not implemented), so if there is already extensive discussion, please point me at it rather than repeating it here.