[Python-ideas] User-defined literals

Andrew Barnert abarnert at yahoo.com
Fri Jun 5 18:13:37 CEST 2015

On Jun 5, 2015, at 08:55, Paul Moore <p.f.moore at gmail.com> wrote:
>> On 5 June 2015 at 16:45, Andrew Barnert <abarnert at yahoo.com> wrote:
>> So, what does "literal" mean, if it's neither the same thing as "compile-time" nor the same thing as "constant" but just happens to overlap those perfectly in the simplest cases? Well, I think the sense in which these things should "act like literals" is intuitively obvious, but very hard to nail down precisely. Hence the intentionally vague "sufficiently simple" definition I gave. But it doesn't _need_ to be nailed down precisely, because a proposal can be precise, and you can then check it against the cases people intuitively want, and see if they do the right thing.
> OK, my apologies, we're basically agreeing violently, then.
> IMO, people typically *actually* want a nicer syntax for Decimal
> values known at source-code-writing time. They probably don't actually
> really think much about whether the value could be affected by
> monkeypatching, or runtime changes, because they won't actually do
> that in practice. So just documenting a clear, sane and suitably
> Pythonic behaviour should be fine in practice (it won't stop the
> bikeshedding of course :-)) And "it's the same as Decimal('1.2')" is
> likely to be sufficiently clear, sane and Pythonic, even if it isn't
> actually a "literal" in any real sense. That's certainly true for me -
> I'd be happy with a syntax that worked like that.

Thank you; I think you've just stated exactly my rationale in one paragraph better than all my longer attempts. :)

Well, I think it actually _is_ a literal in some useful sense, but I don't see much point in arguing about that. As long as the syntax and semantics are useful, and the name is something I can remember well enough to search for and tell other people about, I'm happy.

Anyway, the important question for me is whether people want this for any other type than Decimal (or, really, for decimal64, but unfortunately they don't have that option). That's why I created a hacky implementation, so anyone who thinks they have a good use case for fractions or a custom string type* or whatever can play with it and see if the code actually reads well to themselves and others. If it really is only Decimal that people want, we're better off with something specific rather than general.

(* My existing hack doesn't actually handle strings. Once I realized I'd left that out, I was hoping someone would bring it up, so I'd know someone was actually playing with it, at which point I can add it in a one-liner change. But apparently none of the people who downloaded it has actually tried it beyond running the included tests on 1.2d...)

More information about the Python-ideas mailing list