ruby rocks, python not
Posted by Simon on March 18, 2008 at 09:54 PM
So I'm just doing some python programming, just a few hours, and already I'm missin' ruby.
python dictionary, is a certain key set?
if self.params['foo']:
oh no, that's won't work. Hmm... How about
if defined(self.params['foo'])
Nope... no such luck, there's NO WAY to find out if a variable is defined in python. Finally after futzking around online I find out that the ONLY way to do it reliably in any situation is to—get this—catch an exception. Except since I'm dealing with a dictionary I can use this (undocumented) method:
if self.params.has_key('foo'):
That's so lame. In ruby, you can do any of these:
if @params['foo']
if @params['foo'].nil?
if defined? @params['foo']
Speaking of which, now that I get it, the @foo syntax for an instance variable (stupidly called a "data attribute" in python) is great. So much more obvious & compact than self.foo.
Python also forces you to do some things that I have happily adapted to giving up in ruby. Like calling functions without brackets, isn't
defined? @params
so much prettier than
defined?(@params)
Yes it is. And also, I've started naming my functions with ?s if they yield a boolean and ! if they make a change in place or generally perform a destructive edit—nice.
And ruby's blocks—I love you!
download = Download.new do |d|
d.user = @user
d.name = my_name
end
And
unless and the if/unless modifiers... I could go on forever. Why don't you read the insane book?
This is the official Semacode Weblog!
Subscribe to the full-text RSS feed or the comments RSS feed.
Tags:
