27 Mar 2007

Ruby, Python and Closures

I was talking to someone the other day about the Django vs. Rails web frameworks. These are two similar web frameworks that are written in Python and Ruby, respectively. I had played with Django for a small project once, just to try it out - I like testing web frameworks that are becoming popular from time to time to see if there is a concept that's really clever or that I could use in what I'm working in. I'm pretty framework agnostic in the grand scheme of things - I use Rails because it's the best one I've seen yet and I'm getting pretty good at it, but I'd switch without hesitation if something better came along.
So, I started looking again at Django and there are some big differences but it seems like there's no killer distinction either way. From the little I yet know, I like the built in admin stuff and no baked in JS in Django, I like the migrations and testing support better in Rails. I'll continue to play with Django a bit and see if I can find some more interesting comparisons a bit later. However, one thing I've found right off the bat that seems really huge that I would really miss if I switched over is the lack of closures (blocks). This is a language thing, I haven't used Python in several years and I have been using Ruby for several years now and I've really gotten used to and become quite fond the concept of closures in Ruby. It seems they are basically completely missing in Python. There is a way to sort of hack out comparable functionality, via the lambda function, just like in PHP, but it's really quite ugly.
// RUBY emps.select {|e| e.isManager}
// PYTHON filter(lambda e: e.isManager, emps)

It's not that it's that much longer, it's just that it doesn't read as well. I see these fantastic, clean and simple DSLs being written in Ruby all the time that just makes everything written in it look like pseudocode, where I don't see that at all in Python. I'm also somewhat surprised that there doesn't seem to be iterative classes in Python.
// RUBY array.each do |i| puts i end
// PYTHON for i in array: print i
Why don't collections have generic methods that iterate and aggregate the data? It just seems like it's more procedural in a lot of ways where Ruby takes a much more object oriented approach that looks cleaner and more elegant to me. You can do stuff like this in Python, but it's just not really the same, is it?
Of course, this guy seems to be arguing the opposite, but only uses Zope, which I'm not a huge fan of, to make his point (which even he qualifies).
However, I have read so much about it that is positive and glowing, I feel that I am obviously missing something. I'll continue to play with Django and see if I can't expound.

blog comments powered by Disqus