[Python-Dev] metaclass and author are already decorators (original) (raw)
Paul Morrow pm_mon at yahoo.com
Sat Aug 21 18:29:46 CEST 2004
- Previous message: [Python-Dev] python-dev Summary for 2004-08-01 through 2004-08-15[draft]
- Next message: [Python-Dev] __metaclass__ and __author__ are already decorators
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
[I posted this on comp.lang.python.general but I'm not sure how many of the folks here read that newsgroup, so my apologies if you're seeing this twice.]
Thinking about decorators, and looking at what we are already doing in our Python code, it seems that metaclass, author, version, etc. are all examples of decorators. So we already have a decorator syntax. What is the compelling reason to invent a new one? And if we do, what's to become of the old one?
Here's my take on this. We have two kinds of decorators: those that are informational only (like author and version), and those which have side-effects, i.e. those that actually do something (like metaclass).
class Foo:
""" This describes the Foo class as normal. """
__metaclass__ = M
__author__ = 'Paul Morrow'
__version__ = '0.1'
__automethods__ = True
def baz(self, a, b):
""" This describes the baz method. """
__synchronized__ = True
__returns__ = None
__author__ = 'Neville Shunt'
# body of baz goes here...
There, that looks pretty clear and pythonic. Now how to define the decorators.
def metaclass(decoratedClass, subType):
""" This describes the 'metaclass' decorator. """
__decorator__ = True
__version__ = '1.9'
# perform the metaclass operation on decoratedClass
def synchronized(decoratedFunc, trueOrFalse):
""" This describes the 'synchronized' decorator. """
__decorator__ = True
__author__ = 'Martin Curry'
__version__ = '0.5'
# perform the synchronized operation on decoratedFunc
def returns(decoratedFunc, *args):
"""Docstring for 'returns' decorator."""
__decorator__ = True
# body of decorator goes here
Each decorator function receives the class|function|method being decorated as the first parameter of the function (e.g. decoratedClass and decoratedFunc above). The remaining parameters are the values assigned to the xxx variable in the definition of the decorated function.
So, in the above example, when the 'returns' decorator function is called to decorate the 'baz' method, decoratedFunc would recieve the baz object and args[0] would be set to None (because of the statement "returns = None" in the definition of baz).
For a function to be used as a decorator function, it must be decorated as such, by setting its decorator attribute to True.
Does this handle enough of the decorator concerns?
Paul
- Previous message: [Python-Dev] python-dev Summary for 2004-08-01 through 2004-08-15[draft]
- Next message: [Python-Dev] __metaclass__ and __author__ are already decorators
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]