Proposal: Magic Constants

Jeff Epler jepler at unpythonic.net
Wed Aug 27 11:15:46 EDT 2003


1.05D vs D("1.05") or D(1.05,2) is a 4-character difference.  In the
case of rationals, 1R/15 vs R(1,15) or R*1/15 is also a tiny difference.

And "I have lots of literals in my code" is not an argument I buy.
I counted tokens in 1299 python files in /usr/lib/python2.2*, and 59865
out of 1838995 of them are numeric literals.  That's only 3% of tokens.
By contrast, there are 168520 newlines, 61591 mentions of "self", 31053
comments, 20335 def statements, and 3673 uses of the name "i".  (The
encodings/ directory is the most numeric-literal-heavy portion of the
code, with 11134 numbers in 47423 tokens.  But these are clearly not
candidates for conversion to magic literals)  Even if all these literals
were converted to a notation that added 4 characters per literal, the
increase would amount to 234k out of 9892k, or 2.4%.

In a large system, how do you manage the suffixes?  Third-party module
A and B may both provide an "R"-suffix rational, implemented incompatibly.

Do you want to add functionality to lists/dicts/tuples? ([1,2,3]S for
Set(1,2,3)?)  If not, why do you think that numeric literals are more
important than lists/dicts/tuples?

Currently, u'' and U'' are identical, as are 0L and 0l.  Will 1R and 1r
be equivalent?

Type-suffixes are unfortunate where they exist in the language, and
they're being eliminated today in the case of 'l', and I think there's
a dream to eliminate the 'u' type-prefix for strings as well.  I view
the 'r' string prefix as a bad idea, too, but unfortunately I don't see
it going away.  Well, I can dream...

Jeff
* there are multiple third-party packages installed here





More information about the Python-list mailing list