4 ms·
Disagree on "REST syntax". Python could do just as well: Sinatra: get '/hi' do "Hello World!" end Python 2: @get('/hi') def hello(): return
by mcav 17y ago
Disagree on "REST syntax". Python could do just as well:
Sinatra:
get '/hi' do
"Hello World!"
end
Python 2:
@get('/hi')
def hello():
return "Hello World"
Python 3:
def hello() -> "/hi":
return "Hello World"
- steveklabnik 17y agoA 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."
- showell 17y agoDecorators and annotations are very useful in Python, but I still like the terseness of Ruby--no need to define "hello()". Of course, Python wins on a different kind of terseness--no do/end--but that is mostly orthogonal.
- steveklabnik 17y agoAnd you could use {} instead of do/end, but a Pythonista wouldn't consider that any more terse.
- showell 17y agoYep, my mention of do/end or squigglies was just a caveat on the Ruby-is-more-terse-in-this-example statement in the context of anonymous functions or blocks. I wish there was a language that won on both terseness metrics--i.e. a Ruby with indentation or a Python with anonymous functions.
- jashkenas 17y agoJust for kicks. In CoffeeScript: get '/hi', -> "Hello World" You can have your cake and eat it too.
- unbracketed 17y agoI've never worried much about which language I use to GET hi. As long as the end result is same, everyone should be feeling good.