Proposal: Magic Constants
Raymond Hettinger
vze4rx4y at verizon.net
Wed Aug 27 11:24:56 EDT 2003
[Chris Gonnerman]
> The gist is this: Constants would be allowed to
> be trailed by any single alphabetic character.
> When compiled, a constant with a nonstandard
> letter code would be stored in the compiled code
> in the form of a text literal, appropriately
> marked.
>
> When the code is executed, and the special
> literal is processed, the interpreter would look
> up a conversion function in an internal list. If
> no conversion is found, a runtime error would be
> raised.
>
> Modules to handle alternate numeric formats, such
> as decimal, fixed-point, rational, etc. would
> include a call to a builtin registration function
> which would allow them to "take over" a specific
> alphabetic specifier.
-1
This should not be done for several reasons:
* Constructors are more explicit: Rational(12, 3)
* The appearance unpleasantly reminds me of VB
* There are potential conflicts between alphabetic specifiers which
results in hard to spot bugs. Doest "1.25D" represent the
decimal class, Dewey decimals, or US dollars?
* Previous discussions on special syntaxes initially found them
to be enticing and then someone would realize that real programs
rarely have more than a handful of constants that would benefit
from the syntax. For instance, an accounting program is not
filled with specific values like $1.83. Instead, it constructs
nearly all of its data from user input or files. The program itself
likely checks for zero and amounts being under $1.00. Those
constants are easily and explicitly codeable with a normal
constructor: Decimal("1.00", 2) or some such.
* The registration process by-passes Python's elegant namespaces
and makes global changes resulting in hard to diagnose effects:
import bookaccounting # Hmm, did this just set D to DeweyDecimal?
import Decimal # Did this just reset the value of D or
was it F?
. . .
if foreign_currency:
import Francs
val = 15F # Was this the original F (for Fixed)
or the
# new F (for Francs) ?
# Does it automatically convert
from Euros?
* Guido is already uncomfortable with the number of kinds of string literals:
'abc', u'abc', ur'abc', r'abc', etc. He is unlikely to adopt any related
ideas
for numbers.
Other than that, it is a great idea ;-)
Raymond Hettinger
More information about the Python-list
mailing list