[Python-ideas] User-defined literals

Bruce Leban bruce at leban.us
Wed Jun 3 03:50:53 CEST 2015

On Tue, Jun 2, 2015 at 12:03 PM, Andrew Barnert via Python-ideas <
python-ideas at python.org> wrote:

> Any number or string token followed by a name (identifier) token is
> currently illegal. This would change so that, if there's no whitespace
> between them, it's legal, and equivalent to a call to a function named
> `literal_{name}({number-or-string})`. For example, `1.2d` becomes
> `literal_d('1.2')`, `1.2_dec` becomes `literal_dec('1.2')`, `"1.2"d` also
> becomes `literal_d('1.2')`.
> Of course `0x12decimal` becomes `literal_imal('0x12dec')`, and `21jump`
> becomes `literal_ump('21j'), which are not at all useful, and potentially
> confusing, but I don't think that would be a serious problem in practice.

You seem to suggest that the token should start with an underscore when you
write 1.2_dec and  {...}_o but not when you write 1.2d and 1.2jump.
Requiring the underscore solves the ambiguity and would make literals more
readable. I would also require an alphabetic character after the _ and
prohibit _ inside the name to avoid confusion.

1.2_d        => literal_d('1.2')
1.2j_ump     => literal_ump('1.2j')
1.2_jump     => literal_jump('1.2')

0x12dec_imal => literal_imal('0x12dec')

0x12_decimal => literal_decimal('0x12')

"1.2"_ebcdic => literal_ebcdic('1.2')

1.2d         => error
0x12decimal  => error

1_a_b        => error

1_2          => error

I do think the namescape thing is an issue but requiring me to write

from literals import literal_jump

isn't necessarily that bad. Without an explicit import, how would I go
about tracking down what exactly 21_jump means?

The use of _o on a dict is strange since the thing you're attaching it to
isn't a literal. I think there needs to be some more thought here if you
want to apply it to anything other than a simple value:

(1, 3, 4)_xyzspace
{'a': 1 + 2}_o
{'a', 'b': 3}_o

("abc", "def")_x
"abc" "def"_x

("abc" "def")_x
("abc" "def",)_x

--- Bruce
Check out my new puzzle book: http://J.mp/ingToConclusions
Get it free here: http://J.mp/ingToConclusionsFree (available on iOS)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150602/593dbc35/attachment.html>

More information about the Python-ideas mailing list