[Python-ideas] Proposal for new-style decorators

Christophe Schlick cschlick at gmail.com
Tue Apr 26 19:11:23 CEST 2011


Part 3 - Implementation and additional remarks/questions

I've implemented the idea of NSD about nine months ago and have used
them successfully in many different situations. My first
implementation was terribly messy but at least it did the job. Last
week, while thinking about refactoring, I realized that the whole
process can be easily implemented as a state machine. This has led me
to an extremely compact implementation (about 30 Python statements),
where a callable class repeatedly returns reference to itself, until
it gets all the required parameters to generate and return the
decorated function.
Here is the code, so that you can experiment with it, if you wish:

#---
class decorator(object):
  """apply a 'new-style' decorator to a function"""
  def __init__(self, deco=None, **keys):
    # either get reference or default parameters for decorating function
    self.deco = deco; self.keys = keys; self.stack = list()
  def __call__(self, func=None, **keys):
    if self.deco is None: # get reference for decorating function
      self.deco = func; return self
    elif func is None: # get modified parameters of decorating function
      self.stack.append(keys); return self
    else: # get undecorated function and generate decorated function
      deco = node = lambda *args, **keys: self.deco(deco, *args, **keys)
      deco.func = func; deco.deco = self.deco; deco.__dict__.update(self.keys)
      if self.stack: deco.__dict__.update(self.stack.pop())
      head = '<deco>'; deco.__name__ = name = head + func.__name__
      level = name.count(head); offset = len(head)*level; tail = '.func'*level
      doc = "use help(%s) to get genuine help" % (name[offset:] + tail)
      while hasattr(node, 'func'): node.__doc__ = doc; node = node.func
      return deco
#---

The simplicity of the new implementation has convinced me that it
might be useful to share this idea and write a proposal in order to
get some feedback from the community. As said in the introduction,
this is my first post to python-ideas, so I'm not sure about the
correct process to follow. I've got plenty of questions anyway:

* Is the idea interesting enough to deserve consideration for possible
inclusion in the language? If yes, should I transform this proposal
into a PEP, or should there first be some pre-PEP discussion here (or
maybe in python-dev)?

* Are there some pitfalls involved with the use of NSD that I haven't
seen? Or are there additional desirable elements that could be easily
included?

* After having read this proposal, has anybody some suggestion for
alternative syntax that offer similar features?

* There are some advanced features offered by the new syntax (such as
meta-decorator, or whatever you call them), which seem to be powerful
but are less stable than the elements presented here. I did not detail
this kind of stuff because I consider that it is likely to create some
noise in the discussion, but maybe not?

Thanks for any opinion,
CS



More information about the Python-ideas mailing list