A few more Python thoughts from a Perl guy
So recently I’ve been playing around with the Python, even bought a book to make things slightly less haphazard. First I want to get it out of the way, I like programming in Python – more than Ruby when I dabbled in it (surprisingly, to me, actually) and much more than any time spent with PHP.
Fundamentally, I think there’s a vast philosophical difference between Perl and Python which I need to internalize and will spare me much frustration. Perl comes from a culture and evolutionary standpoint of Do What I Mean. Perl tries to let you type in some stuff and it does generally what you probably mean to do. I think this bothers some people. Python programmers probably, because although as far as I know they haven’t encodified this the way Perl has DWIM, Python is a Do What I Say kind of language. You need to be pretty explicit with it. I don’t think there’s a right or wrong in this argument, just a philosophical difference.
Now on to saying stuff.
The indenting thing, I’m totally cool with it. In fact sometimes when I’m doing perl and things get a little hairy with a few adjacent close parens, I’m like, huh, that looks messy. On the other hand, I really, really like being able to put my cursor on a paren and shift-5 to have my cursor jump to its mate (you know, Vim style). That’s really useful.
Something really bothers me, though. I’ve read a few times in the book where they’ve built in a feature with one of its implementation goals to discourage the use of that feature. What? This is totally stupid, either implement the feature full on because you need it and it is a good idea or don’t because you don’t believe in it. For example, the ternary operator, well beloved by this developer. In other languages with it, they’ve cribbed the C version – in Perl for example:
$is_zombie = undead() ? 'yes' : 'grr. argh.';
but in Python they have to say:
is_zombie = 'yes' if undead() else 'grr. argh.'
And the reason for this change of syntax? Quoted from the footnote:
This was reportedly done in response to analysis of common use patterns in Python code, but also partially to discourage ex-C programmers from overusing it!
I mean, that’s literally the dumbest thing I’ve ever heard. Ok. Not literally. But it’s dumb. There’s nothing wrong with the ternary operator it’s very easy to understand once you know what it means and it’s a concise efficient way to code a lot of things.
Another example of this is Python’s hobbled lambda functions. Anonymous functions are very useful, you can easily make them and pass them around without worrying about naming and all that extra typing. But in Python these functions are limited to exactly one expression. That’s right. The function can consist of one expression. What? Why?
Because it is limited to an expression, a lambda is less general than a def—you can only squeeze so much logic into a lambda body without using statements such as if. This is by design—it limits program nesting: lambda is designed for coding simple functions, and def handles larger tasks.
Oh. This is what I mean when I talk of holier than thou languages. Languages that tell you exactly how you should be programming. Right after that in the same section of the book the author tells you how to cheat this by using list comprehensions and what not to get more complicated logic into the lambdas, presumably because it is often good to do so, but then says you should worry about obfuscating your code. Of course, because the language forces you to do so to use this construct, these hacks increase the obfuscation of the code. Great job.
They also have a lot of that inverted loop/branching logic like the ternary operator – but only for special cases. Another example is in their list comprehensions where you can say something like:
squared_nums = [ x**2 for Range(5) ]
Which is nice and concise. By why not offer that syntax outside of list comprehensions? I mean, it’s just nice to be able to say things like:
continue if test_something()
sys.stdout.write(line) for line in open('file')
That is, though, just the Perl developer in me talking.
In general I like developing in Python, as is obvious, I miss things from Perl (*cough*labeled blocks*cough*block scope*cough*) but I’m enjoying switching my mode of thinking. Learning new languages is always good and makes you better at programming in general and it’s been awhile since I’ve really had a go at it. I guess I have Google and App Engine to thank for that (although App Engine itself isn’t without it’s frustrations for me! Again, I suspect a mindset change is required).