[Python-ideas] User-defined literals

Terry Reedy tjreedy at udel.edu
Wed Jun 3 06:48:47 CEST 2015

On 6/2/2015 9:56 PM, Andrew Barnert via Python-ideas wrote:

> The problem is that Python doesn't really define what it means by
> "literal" anywhere,

The reference manual seems quite definite to me. The definitive section 
is "Section 2.4. Literals". I should have all the information needed to 
write a new implementation. It starts "Literals are notations for 
constant values of some built-in types."

The relevant subsections are:
2.4.1. String and Bytes literals
2.4.2. String literal concatenation
2.4.3. Numeric literals
2.4.4. Integer literals
2.4.5. Floating point literals
2.4.6. Imaginary literals

 > and the documentation is not consistent.

I'd call it a bit sloppy in places.

> There
> are at least two places (not counting tutorial and howtos) that
> Python 3.4 refers to list or dict literals. (That's not based on a
> search; someone wrote a StackOverflow question asking what those two
> places meant.)

Please open a tracker issue to correct the sloppiness and reference the 
SO issue as evidence that it confuses people.

> Which I don't actually think is much of a problem. It means that in
> cases like this proposal, you have to be explicit about exactly what
> you mean by "literal" because Python doesn't do it for you.

Again, the Language Reference seems sufficiently explicit and detailed 
to write another implementation. 2.4.3 says

"There are three types of numeric literals: integers, floating point 
numbers, and imaginary numbers. There are no complex literals (complex 
numbers can be formed by adding a real number and an imaginary number).
Note that numeric literals do not include a sign; a phrase like -1 is 
actually an expression composed of the unary operator ‘-‘ and the 
literal 1." I will let you read the three specific subsections

> This is similar to the fact that Python doesn't actually define the
> semantics of numeric literals anywhere.

I am again puzzled by your claim. There are 3 builtin number classes: 
int, float, and complex.  There are 3 type of numeric literals: integer, 
float, and imaginary. "An imaginary literal yields a complex number with 
a real part of 0.0."  Anyone capable of programming Python should be 
able to match 'integer' with 'int' and 'float' with 'float.

Terry Jan Reedy

More information about the Python-ideas mailing list