Underscores in Python numbers

Roy Smith roy at panix.com
Tue Nov 8 10:39:09 EST 2005


Dave Hansen  <iddw at hotmail.com> wrote:
> Of course, I write _far_ more code in C than Python.  But I've seen
> enough bugs of the sort where someone wrote 1200000 when they meant
> 12000000, that I see great value in being able to specify 12_000_000.

I'll admit that being able to write 12_000_000 would be convenient.
On the other hand, writing 12 * 1000 * 1000 is almost as clear.  In C,
the multiplication would be done at compile time, so it's not even any
less efficient.  I'm not sure how Python handles that, but if it
turned out to be a serious run-time performance issue, it's easy
enough to factor it out into something that's done once and stored.

Bottom line, embedded no-op underscores in numbers would be nice (and,
IHMO, should be added), but the lack of such a feature should not be
used as an excuse to write such unreadable monstrosities as 12000000
in source code.

Semi-related: see Jakob Nielsen's complaint about having to enter
credit card numbers as 16-digit strings with no breaks on web forms
(http://www.useit.com/alertbox/designmistakes.html, item #7, last
bullet point).



More information about the Python-list mailing list