4 ms·
in Python one function definition inside another is totally normal, and (to my knowledge) the statement doesn't generate a global binding, so that the inner-def
by thaumasiotes 13y ago
in Python one function definition inside another is totally normal, and (to my knowledge) the statement doesn't generate a global binding, so that the inner-defined function is not accessible from outside the outer one. In other words, python's def appears to be syntactic sugar for variable assignment within whatever scope you're already in, except of course that there is no nonsugared form. Which would make it analogous to let, I believe.
- babarock 13y agoTechnically, in Python nothing prevents you from writing something like: foo = lambda x: x*2 If only that lambda statements are limited (by design?) to only a single return expression. As far as Hy goes, lambdas won't have this limitation.
- icebraining 13y agoIn a way, there is: >>> mycode = "a = int(raw_input()); print a * 2; return a" >>> myfunc = FunctionType(compile(mycode, "<string>", "exec"), globals()) >>> myfunc() 4 8 4
- wging 13y agoThis SyntaxErrors for me. >>> from types import * >>> mycode = "a = int(raw_input()); print a * 2; return a" >>> myfunc = FunctionType(compile(mycode,"<string>","exec"), globals()) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1 SyntaxError: 'return' outside function Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1 SyntaxError: 'return' outside function