Re:Code blocks in Python
by hythloday on Friday April 20, 2012 @10:32AM
(#3868013)
But you don't have to write your callbacks in reverse order in python:
Sure, if you want to call if_but outside a function, it has to be defined, but inside a function (as in your example, and in 99% of python development that doesn't occur in a REPL) x() is effectively globals()['x'](). Have I totally misunderstood your point?
[ Parent
| on HN ]
This problem is indeed less severe when the names are defined in a non-local namespace (inside a class, or as globals).
But callback-style often appeals to defining very-local callback code. In fact, it would be anonymous in languages that support it, but it is forced to not only be given a name, but also potentially clutter some namespace (or be written in reverse order).
Consider having every for/while loop, or every "if" require giving a name to the code block[s] within it. It sounds insane. The same doesn't sound insane for library functions, but in my opinion that's really just because everybody's used to this limitation.
When I moved from Python to Haskell, one of the many joys was that I could stick an anonymous code block anywhere so easily.