[Python-Dev] method decorators (PEP 318)
Shane Holloway (IEEE)
shane.holloway at ieee.org
Mon Mar 29 15:20:12 EST 2004
I think one important thing to keep in mind is that many decorated
methods end up being very similar, and hence you would want the syntax
to allow for compressing them. For instance, one would want to be able
to write the following code block:
class Example(object):
def foo(self, *args) [
synchronized(lock),
attributes(author='SWH', protocol=IMyProtocol),
myArgumentWireMarshaller,
]:
pass # Code goes here
def bar(self, *args) [
synchronized(lock),
attributes(author='SWH', protocol=IMyProtocol),
myArgumentWireMarshaller,
]:
pass # Code goes here
def baz(self, *args) [
synchronized(lock),
attributes(author='SWH', protocol=IMyProtocol),
myArgumentWireMarshaller,
]:
pass # Code goes here
more like:
class Example(object):
IMyProtocolMethod = [
synchronized(lock),
attributes(author='SWH', protocol=IMyProtocol),
myArgumentWireMarshaller,
]
def foo(self, *args) [methodreturns(float)] + IMyProtocolMethod:
pass # Code goes here
def bar(self, *args) [methodreturns(str)] + IMyProtocolMethod:
pass # Code goes here
def baz(self, *args) [methodreturns(int)] + IMyProtocolMethod:
pass # Code goes here
I think that as decorators get used more and more, we will want some way
to shorthand a long list of common decorators. Note that I'm not sold
on the exact syntax, just the need for the shorthand in a maintainable
system. After all, copy and paste gets very sticky over time.
Thanks,
-Shane
More information about the Python-Dev
mailing list