Tuesday, August 4, 2009

Python on Python

Yesterday my python slithered off, mad because I was writing Ruby. I feared he'd swallow the kitten. Since Python lets you create attributes on the fly, I solved the problem:


python.diet = 'vegetarian'


Actually, Python has nothing to do with snakes. The name comes from the British comedy troupe, Monty Python. My python is named Monty. Ruby gets its name from lists, such as wedding anniversaries, where Ruby follows Pearl.

When Python and Ruby engineers debate the merits of their respective languages, the discussion is generally learned and respectful. Monty, on the other hand, can really get going.

"Hey, Monty! Want to talk about Tim Toady?" (He never passes this one!)

"There you are. Tell me what you think of Tim."

"Martin, Tim's a fraud. There's maybe sixteen different ways to loop through an array in Ruby. You got some hotshot coder who never does it the same way twice. OK, boss. Who's going to maintain that crap? Some junior coder who just learned Ruby? I don't think so. You're stuck with hotshot. Hope you like paying his six-figure salary."

"Let's show a little real code." Monty dictates a little Ruby and Python.
Python



ids=[...] # array, listing division's employee ids

for id in ids
    # code processing
    # each id

Ruby


ids=[...] # array, listing division's employee ids

ids.each { |id|
    # code processing
    # each id
}


"You could write that loop other ways in Python, but no one would. That's the one, obvious way to do it. Ruby dudes will write every loop differently to show off their Tim Toadiness. And no, I did not forget curly braces in Python. You indent and you get code that looks like, and is, blocked."

"I could go on, Martin, but I just got this sudden urge for some broccoli. Broccoli? I never ate broccoli. What's going on?"

Monty slithers off. Actually, in Ruby looping by the "each" method is the preferred way, in most instances. The simplicity of the Python illustrates the quality that Pythonistas call "pythonicness."

1 comment:

  1. Ahh yes, Timmy is quite prevalent in Ruby code and his corrupting influence shall haunt us for many years to come in the form of unmaintainable Ruby and Perl code :(

    Python is simplistic beauty divine. I have at times questioned Guido's direction -- most recently with the Python 3000 release. But i have come to see the genius in Python 3000.

    Pythons single way to loop collections is brilliant. If you need a one line "each" like syntax use an LC.

    >>> [x.upper() for x in ('one', 'two', 'three')]
    ['ONE', 'TWO', 'THREE']

    ...is syntatcic sugar for...

    >>> l = []
    >>> for x in ('one', 'two', 'three'):
    l.append(x.upper())

    >>> l
    ['ONE', 'TWO', 'THREE']

    ReplyDelete