5 ms·
As 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. I
by fuzzyman 17y ago
As 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