[Python-Dev] pep 318, Decorators for Functions,
Methods and Classes
Jack Diederich
jack at performancedrivers.com
Wed Aug 11 21:44:44 CEST 2004
On Tue, Aug 10, 2004 at 09:11:16PM -0400, Jack Diederich wrote:
> On Sat, Aug 07, 2004 at 02:05:14PM -0700, Guido van Rossum wrote:
> > > I just wanted to say that I believe it should be allowed to decorate
> > > classes. There's not reason enough (reading the thread linked to by the
> > > PEP) to limit decorators this way. Like said several times in that
> > > thread, metaclasses are harder (to write and to understand) than
> > > decorators.
> >
> > Are you volunteering to implement it?
> >
> I'm game, I would prefer to use class decorators in most of the places
> I currently use metaclasses.
I have a mostly working patch for class decorators but it gets an XXX ambiguity
between
funcdef: [decorators] 'def' NAME parameters ':' suite
and
classdef: [decorators] 'class' NAME [ '(' testlist ')' ] ':' suite
It works as advertised with the pre-decorators funcdef definition (which removes the ambiguity).
Help?
-Jack
# --- quicktst.py ----
class Tom(object): pass
def decor(cls):
print "decord'd", cls
return Tom
@decor
class Dick(object):
""" some classs """
def bar(self):
print "BAR!"
return
@decor
class Harry:
def bar(self):
print "BAR!"
return
print Dick
print Dick.__class__
print Harry
print Harry()
print Harry().bar()
./python
Python 2.4a2 (#13, Aug 11 2004, 15:25:00)
[GCC 3.3.4 (Debian 1:3.3.4-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import quicktst
decord'd <class 'tst.Dick'>
decord'd tst.Harry
<class 'tst.Tom'>
<type 'type'>
<class 'tst.Tom'>
<tst.Tom object at 0x402c23ec>
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "tst.py", line 25, in ?
print Harry().bar()
AttributeError: 'Tom' object has no attribute 'bar'
>>>
More information about the Python-Dev
mailing list