[Python-Dev] PEP 318 trial balloon (wrappers)

Jewett, Jim J jim.jewett at eds.com
Thu Mar 11 18:50:34 EST 2004


----------
class Foo:
    [transform] from:
        def x():
            pass
 
[wrapper] from:
    class x:
        pass

[decorator] from:
    def x():
        pass

x() is now defined exactly as it was before,
except that it has an extra indent to the right.

---------

I understand most of the objections well enough to
summarize them; I'm singling these out because I'm 
not sure I that I do.

Phillip J. Eby:

> It is ambiguous precisely *because* it introduces a new suite.  
> Suites in Python indicate a difference in execution context, 
> often along with introducing a new namespace.  This new syntax
> does neither.

Today, if we wait until the declaration is finished, we can write

    for decorator in seq:
        var = decorator(var)

How does this construct change the context or namespace any less
than a for loop?


Raymond Hettinger:

> This gains nothing over what we have now

It moves the transform near the "def", and it indicates that
there will be something unusual about this definition.  
(staticmethod or classmethod change the expected signature,
other wrappers may turn it into a non-callable, etc.)

> ... and limiting the ability to apply a
> decoration outside the class definition.

No syntax will improve on the current idiom if you want to
change class attributes from outside the class.

Are you assuming it can't apply to classes or functions 
because I happened to choose a method example this time?

If not, I'm afraid I don't understand this objection.

> In its favor, this syntax does suggest something
> new which is the possibility of having all similar
> decorations grouped under one banner:

> class Foo:
>    [classmethod] from:
>        def bar():
>            pass
>        def bat():
>            pass
>        def baf():
>            pass

Would you believe I hadn't even thought of that?

That also might be a bit trickier to parse, since
it would require a cartesian product instead of 
just an analogue of reduce.

> That being said, grouping doesn't go much for me.  
> I think the key to the previous proposals was putting 
> wrappers on the same line as the function definition. 

I think the key was associating them more closely.

Mentally, when I see the indentation go back (or the
parentheses close), I shove that object out of my
mind.  The current idiom is like saying "oh, wait,
I forgot.  I didn't finish the definition yet!"

Putting the decorator on the same line is good, but
putting the declaration inside the decorator's context
meets the same goal.  (Perhaps not as well, depending
on taste.)

-jJ



More information about the Python-Dev mailing list