[Python-Dev] Re: [Python-checkins] python/nondist/peps pep-0318.txt, 1.25, 1.26

Skip Montanaro skip at pobox.com
Tue Aug 24 21:59:33 CEST 2004


    >> List some possible reasons why arriving at consensus about decorators
    >> has been so hard (or impossible) to acheive.  There are certainly
    >> more.

    Neil> Perhaps you could add my reservation (objection is too strong a
    Neil> word).  I think decorators are not powerful enough given their
    Neil> high syntactic profile.  This could be rephrased as "if we are
    Neil> going the use the @ sign then it should be able to do really cool
    Neil> things".

I was trying to elaborate general barriers to concluding this process, not
issues related to selecting one proposal over another.

    Neil> One idea is to have the compiler pass the AST for the function
    Neil> body to the decorator function.  The decorator could create new
    Neil> nodes in the AST or modify existing ones.  That would allow
    Neil> decorators to do things like adding a try/except without
    Neil> introducing another function call.  The output of the decorator
    Neil> would be passed to the code generator.

I suppose you could do that, although if you did I suspect metaclasses would
seem tame by comparison.  Couldn't you do something like this?

    @ast_to_func
    @neils_cool_ast_transform
    @func_to_ast
    def f(a):
        pass

After all, there's no restriction on what a decorator can return.

How about we save true AST decorators for "$"? <wink>

Skip


More information about the Python-Dev mailing list