A Python (non-)idiom that I should really avoid

December 17, 2010

One of my bad habits as a Python programmer is that I am both lazy and impatient. I will go out of my way to do something overly clever just because it saves me some boring boiler-plate code, and because Python is such a compact language already my threshold for this irritation is very low.

One of the things that this leads me to is writing conditional variable initializations like this:

var = None
if condition:
    var = something()

I've recently come to the conclusion that this is a bad idiom that I should avoid. On the 'good' side, I've saved a line and the annoyance of writing the boiler-plate if/else (at the trivial cost of an extra assignment). On the bad side, what I've found recently is that that the 'var = None' assignment can easily look a little bit too much like it could have been a temporary addition done for debugging or code testing. In fact I almost removed such an initialization today during a cleanup pass of stripping my debugging and testing code from a program before committing the changes to the repository. Actually putting the else: in makes it clear to me that both initializations are supposed to be there.

(Of course there's all the general style and 'clever: -5 points' issues involved with writing code like this. But, well, I'm lazy and impatient and so I sometimes do this stuff anyways.)

I suppose I should be thankful that I don't really like Python's conditional expressions, and also that I'm still writing code for Python 2.4.6, which doesn't have them so I don't have a choice about liking or not liking them. Writing the above as:

var = something() if condition else None

is just far too clever, partly because I find that the order makes it hard to read.

Comments on this page:

From at 2010-12-18 05:59:11:

That’s what happens when a language doesn’t have variable declarations: people want them anyway. In Perl it’s very natural to write

   my $var;
   $var = something() if $condition;

I find the else is what primarily bungles the readability of the the Python version; statement modifiers are eminently readable to me as long as the conditional part is short. In fact if it’s very short, and the condition very long, I find the statement modifier version more readable than a regular conditional. I.e if something() stands for a long expression,

   my $var;
   if ( $condition ) { $var = something() }

There’s still nothing that suggests “for debugging” in this case.

Doesn't Python at least have ternaries? In Perl I might be inclined to alternatively write this:

   my $var = $condition
       ? something()
       : undef;

Aristotle Pagaltzis

By cks at 2010-12-18 09:07:19:

What you're calling ternaries are what I called conditional expressions. Python 2.5 has them, but I'm still writing code for Solaris (with Python 2.4.6) and anyways I don't like the syntax.

From at 2010-12-29 19:52:12:

I often resort to boolean expressions to initialize this sort of variable, e.g.:

   var = condition and something() or None

This is not a very big improvement over the two-part expression, with the None initializer, but it keeps all the related bits of the same code in one expression; even if I decide to split the expression in multiple lines, e.g. for readability:

   var = condition and something() \
           or None
Written on 17 December 2010.
« Sometimes bugs have very small edit distances
A tale of memory allocation failure »

Page tools: View Source, View Normal, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Fri Dec 17 23:55:14 2010
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.