[Python-3000] Brainstorming: literal construction hooks
Guido van Rossum
guido at python.org
Sat Apr 22 10:24:27 CEST 2006
It's an interesting idea; it's been brought up before but nobody AFAIK
has ever implemented it. I like the scoping requirement. I suggest you
try to implement it and see how well it works. You probably also want
to be able to hook list, tuple and dict displays (perhaps by supplying
an alternative factory function).
Good lock!
--Guido
On 4/22/06, Michael Urman <murman at gmail.com> wrote:
> This idea isn't fully fleshed out, but I wanted to air it to see if it
> took wind or fell flat. Please forgive inaccuracies between lexing and
> parsing.
>
> It's about being able to override what a given literal is turned into.
> It would only take effect in a limited scope, either per module, per
> compile/exec, or something similar. When a literal of a registered
> token type is parsed, its string would be passed to the provided
> function and whose return value would be used for the object. The
> following example, when registered, would be used to turn all float
> literals into decimal instances.
>
> def float_literal(literal):
> return decimal.Decimal(literal)
>
> More simply decimal.Decimal could be registered as the literal
> constructor if just the literal's string is used. Alternative
> signatures could allow a single function to handle multiple token
> types, or could make applying modifications to normal types simpler,
> but would prevent the obvious use of decimal.Decimal as above. I see
> token_type and value (as normally parsed) as obvious candidates for
> this.
>
> This could be used for people who want to create auto-substituting
> string literals (which I dislike for I18N reasons) without having to
> shoehorn its own syntax into the language.
>
> There's a lot of holes to this story, including at least how these
> functions are registered, and which additional arguments (if any) are
> necessary. Shall we try to fill these holes in?
>
> Michael
> --
> Michael Urman http://www.tortall.net/mu/blog
> _______________________________________________
> Python-3000 mailing list
> Python-3000 at python.org
> http://mail.python.org/mailman/listinfo/python-3000
> Unsubscribe: http://mail.python.org/mailman/options/python-3000/guido%40python.org
>
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-3000
mailing list