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

But you can define functions inside of functions (recursively).

  def function1():
      print ("hello outer")
      def function2():
          print ("hello inner")
      function2()

  function1()
Output:

  hello outer
  hello inner


But you can’t do that (or try/except) in an expression such as a list comprehension or similar.


Yes, because gross.


Nor, IMO, should you want to.


Why not? Many other languages have closures and even Python has lambdas. Multi-line closures work well for these languages including in map/reduce/etc or comprehensions where applicable. Seems like your preference is overfit to Python’s limitations.


Because you can do it in a function. It's not a limitation when you can do it. Having to define a function that's easier to read, and testable as an independent unit, is not a problem.




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

Search: