3 ms·
A Rubyist would argue that that Python 2 syntax is anything but clear. I have no idea what that Python 3 snippet is doing... care to share? That looks like som
by steveklabnik 17y ago
A Rubyist would argue that that Python 2 syntax is anything but clear.
I have no idea what that Python 3 snippet is doing... care to share? That looks like something that's hard to Google...
- fuzzyman 17y agoAs a Pythonista I have no idea what the Ruby snippet is doing. Readability depends entirely on context. 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.
- steveklabnik 17y ago> As a Pythonista I have no idea what the Ruby snippet is doing. 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.
- algorias 17y ago> 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.
- steveklabnik 17y agoAh. So, Python uses the Java annotation syntax for decorators? Hm. One more thing I'll have to look into when I get the time.
- rbanffy 17y agoActually, I think Java uses Python decoration syntax for annotations, but I have been wrong before.
- pmiller2 17y agoI'm pretty sure Python picked it up after Java did. But, I don't think it's a case of imitation; it's just that the glyph '@' happened not to have syntactic meaning attached to it and it looks okay.
- blasdel 17y agoNo, they're exactly the same beast! I've used decorators to add attributes to function objects in several projects in the past -- you don't have to wrap the original.
- aerique 17y agoAs a Pythonista I have no idea what the Ruby snippet is doing. Readability depends entirely on context. 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 :)
- rbanffy 17y ago> Oh, come on. You're just disagreeing for effect here. 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.
- steveklabnik 17y ago"get" is a function that takes a string and a block. And the block is bound to the uri given in the string, so your guess is correct.
- rbanffy 17y agoRuby always confuses me with this flexible syntax...
- steveklabnik 17y agoIt's possible that you just haven't read enough ruby, then. There are only two variations on the syntax of passing a block to a function, "do/end" and "{ }". By convention, multiline blocks use do/end, and single line blocks use {}. It's also possible that one of the things in the article is the gotcha, flexible parenthesis. By convention, Rubyists tend to use parenthesis when defining functions, but not when calling them: def get(path, opts={}, &block) get "/hello" do not get("hello") do Which would still work, if you preferred. The exception to this is chaining method calls: Users.find_all_by_age(23).select {|user| user.first_name == "steve" } ... not that that's an awesome use of ActiveRecord, but whatever.
- 17y ago
- mcav 17y agoIt's using function annotations as a way to describe which URL to route to. Function annotations aren't used by anything in Python3 by default, so it's one possible way to do that.
- rbanffy 17y agoI would prefer to use a decorator for that.
- chromatic 17y ago> A Rubyist would argue that that Python 2 syntax is anything but clear. Why does that matter? The syntax should be clear to a capable Python developer, not someone who has never used Python. I'm not suggesting that that particular syntax is or is not clear to said Python programmer, merely that the consideration for a Ruby programmer is useless.
- steveklabnik 17y agoBecause "he's a Rubyist" is the reason he would say something like "you can't have Sinatra's clear syntax."