by fuzzyman · 16y ago · 0 comments
The Python 3 version is using a function annotation.
In Python 2 or 3 you could use a context manager (with statement) to similar effect.
Oh, come on. You're just disagreeing for effect here.
I've never used Ruby but that snippet is clear as day.
The Python 3 snippet suggests to me that the output would be "/hi" though :)
Oh, come on. You're just disagreeing for effect here.
I've never used Ruby but that snippet is clear as day.
The hell it is. I find it quite confusing. I'll take the Python 2 solution anyday, which clearly seperates what is called from when it is called. One of the main problems with the 'my language is so flexible' approach is that inventing concise solutions becomes a goal in itself.You're passing some code as an argument to a function. Many other languages do this. Do you hate Lisp and Haskell, too? Do you not use continuations in Python? Or callback functions?
def foo(f):
f()
or (define (foo f) (f))
That has nothing to do with annotating a function to show it binds to some URL and is invoked when an HTTP GET request is received with that URL. I don't like having to scroll through a source file to discover such bindings and I actually prefer them to be defined at a separate place. In that sense, the Python 2 solution isn't good either and suffers from the same problem I described earlier: people try to be so concise that they loose sight of other goals.It's unclear what "get" is. Is it a function? What does it return? My guess would be that it returns a function bound to the handler of a "/hi" url, but that's a guess.
And the block is bound to the uri given in the string, so your guess is correct.
Fair enough. Arguing over beauty is silly anyway, I just wanted to point out that the entire thing is entirely subjective.
> The Python 3 version is using a function annotation.
Thanks, checking out PEP 3107 now. I've always seen annotations done with the @annotation syntax like the Python 2 snippet has.
The @ syntax is for decorators. Different beast altogether.
I've used decorators to add attributes to function objects in several projects in the past -- you don't have to wrap the original.