Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


Technically, 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.


I totally write code like this all the time, when I need a quick one-liner that I don't really care about. A little more then a technicality.


Totally. Hy will even auto-expand a lambda to a function if it's not sane Pythonically :)


In 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


This 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




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: