[Python-Dev] PEP 318: Decorators last before colon
Shane Hathaway
shane at zope.com
Tue Mar 30 23:08:05 EST 2004
On 03/30/04 16:21, Guido van Rossum wrote:
>>Another possibility that has been suggested is
>>
>>[decorator]
>>def func(arg, arg):
>
> And one that I currently favor. I'm out of bandwidth to participate
> on a msg-by-msg basis, but perhaps folks can see if they can come to
> terms with this solution?
+1. We had a short email exchange about this a while ago. I'm glad
it's back on the table. It's elegant, and the decorations people are
already using will become more apparent than they are today.
This is important to me because decorators need to be very visible. One
class I frequently use (SimpleVocabulary in Zope 3) drove me crazy at
first until I understood the pattern the author had used. The
constructor takes two strange arguments, and for quite a while I
couldn't figure out just what it wanted. Finally, I noticed that the
class has several classmethods, and they all call the constructor. The
author intended users to call the classmethods, not the constructor, but
it was hard to notice any classmethods since the word "classmethod" was
buried below the function body. Using "cls" as a first argument didn't
help, since I've practically trained my eyes to ignore the first argument.
Zope has experimented with several ways of decorating methods with
security declarations. Here are some of the variations attempted so far:
class Foo:
__ac_permissions__ = (('bar', 'View management screens'),)
def bar(self):
pass
InitializeClass(Foo) # Finds __ac_permissions__ and changes methods
class Foo:
bar__roles__ = PermissionRole('View management screens')
def bar(self):
pass
class Foo:
security = ClassSecurityInfo()
security.declareProtected('View management screens', 'bar')
def bar(self):
pass
InitializeClass(Foo) # Finds a ClassSecurityInfo and calls it
These are all bad enough that Zope 3 has chosen to make no such
declarations at all in Python code, putting them in XML instead. That
may be the right choice for Zope 3, but surely other Python developers
are running into similar needs on their own projects. They shouldn't
have to go through this pain. They should be able to start with
something clean like this:
class Foo:
[protected('View management screens')]
def bar(self):
pass
Shane
More information about the Python-Dev
mailing list