Good code patterns in Python

Theodor Rash trash at kjn-law.de
Thu Jul 3 10:46:05 CEST 2003


Andrew Bennetts wrote:

> 
> if cond:
>     x = special_val
> else:
>     x = some_val
> 
> can be "read" more-or-less directly into English: "if the condition is
> true, then 'x' is set to 'special_val'; otherwise, 'x' is set to
> 'some_val'".
> Saying "'x' is set to 'some_val'.  Now, if some condition is true, 'x' is
> set to 'special_val'" is counter-intuitive to my way of thinking.
> 
> That said, I can readily recognise both idioms (despite having a slight
> preference for one form), so it's really a non-issue...
> 
ACK, but I'd like to add an additional aspect:
If the if-statement should work as a real switch, that is: one branch or the
other of two equivalent branches, than it should be coded in a way that the
reader recognizes the switch at first glance, with if/else.
If the condition is brought into the code to fit some rare exception, for
example to meet some hardware requirements that might get obsolete and
thrown out again in a later stage of maintenance, then the form 'x' is
set to 'special_val' is the right way, IMHO. But it should be accompanied
with a comment that indicates the purpose of that piece of code.
Anyway, instead of speculating about how some other programmer might perhaps
interpret the code that I wrote, I give him an explicit comment and I'm
done.
(Saw this sig somewhere: 'Comment my code?? Why do you think they call it
code?')  
Theo




More information about the Python-list mailing list