Pull Last 3 Months

Ben Finney bignose+hates-spam at benfinney.id.au
Wed Oct 17 21:50:14 EDT 2007


John Machin <sjmachin at lexicon.net> writes:

> It's a bit hard to see how anybody could imagine that in the expression
>     [months[(month - i - 1) % 12] for i in range(n)]
> the number 12 referred to anything but the number of months in a year.

Exactly, that's what people *will* assume. But what if they're wrong,
and you're using the number 12 for some other semantic purpose? If the
programmer has encountered this type of betrayed assumption before,
they'll never be entirely sure that a bare '12' in the code means what
they think it means. And the code doesn't say anything about why the
number was used, so they're left to guess.

Of course, in such a trivial example, it is almost unthinkable that
the number 12 would mean anything else; but the entire point of the
principle of not using magic numbers is that you don't have to wonder
about when that line is crossed.

Better to be explicit about it, in every case, IMO.

-- 
 \         "Money is always to be found when men are to be sent to the |
  `\   frontiers to be destroyed: when the object is to preserve them, |
_o__)  it is no longer so."  -- Voltaire, _Dictionnaire Philosophique_ |
Ben Finney



More information about the Python-list mailing list