[Python-3000] Brainstorming: literal construction hooks

Michael Urman murman at gmail.com
Sat Apr 22 17:34:54 CEST 2006


On 4/22/06, Guido van Rossum <guido at python.org> wrote:
> 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.

I'll look into that now that I know it's not a discard idea.

> You probably also want  to be able to hook list, tuple and dict
> displays (perhaps by supplying an alternative factory function).

Maybe I've not been awake long enough, but I don't see what I'd want
to hook here that isn't already done using a subclass with an
overrided __str__ or __repr__ method.

On 4/22/06, Nick Coghlan <ncoghlan at gmail.com> wrote:
> class MyCompiler(Compiler):
>   def build_float(self, literal):
>      return Decimal(literal)
>
> # Pass this to compile or exec via an ast_compiler argument
> # It may even be possible to permit it as an argument to __import__

I definitely like the look of this for the complie/exec usage, but it
definitely makes for a more annoying general usage. I'm not sure about
the __import__ way, as it smells of spooky effects, but might be a
good practical back door to bring a submodule in line.

For in-module effect, maybe some special import could pull in a
compiler class instance used only for the current module, allowing its
build_xyz methods be replaced.

    from __ast__ import compiler
    compiler.build_float = Decimal    # on an instance, so no 'self' parameter

If something like that sounds reasonable for usage, I'll start looking
into how it looks for implementation. (Probably a big ball of mud! And
sounds like it depends on the python-side interface to the ast - I've
lost track if that's solidified yet.) :)

Michael
--
Michael Urman  http://www.tortall.net/mu/blog


More information about the Python-3000 mailing list