5 ms·
But you can define functions inside of functions (recursively). def function1(): print ("hello outer") def function2(): print ("hello i
by tutfbhuf 6y ago
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
- throwaway894345 6y agoBut you can’t do that (or try/except) in an expression such as a list comprehension or similar.
- robertlagrant 6y agoYes, because gross.
- deleted 6y ago[deleted]
- dragonwriter 6y agoNor, IMO, should you want to.
- throwaway894345 6y agoWhy 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.
- robertlagrant 6y agoBecause 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.