[Python-ideas] __decorate__ method

Zac Burns zac256 at gmail.com
Tue Apr 20 04:37:54 CEST 2010


I would like to propose an extension to behavior the decorator syntax. For
@decorator '@' should see if decorator supplies a __decorate__ method. If
so, call the __decorate__ method. Otherwise, call decorator directly.

Guido does say that __decorate__ should not replace __call__ here
http://markmail.org/message/ig6diu6j55flxr6c, saying

"""
Q: Is there a strong feeling (in any direction) about calling the
decorator's "decorate" or "__decorate__" attribute, rather than its
"__call__" attribute?

A: It should be __call__; that's the only backwards compatible way, and
makes it easy to create decorators as functions.

"""
(Sidenote: Can someone explain what the backward incompatibility is?)

There is one use case I have run into where I wanted to make a distinction
between __call__ and __decorate__. It is a Null object, which is like None
but supplies several interfaces. It's an empty collection, a context that
does nothing, a callable that returns None, and should be a decorator that
returns the original function - except that it's already a callable that
returns None. This object is cool for doing stuff like:
"""
with (blocking if block else Null):
   doSomething()
"""
Rather than
"""
if block:
   with blocking:
      doSomething()
else:
   doSomething
"""
And, it fits in several spots like "os.walk(x, onerror=Null)" for example.

I'll admit, the use case here is fairly spurious. There may be use cases
which are more 'real'. If not, *C'est la vie*.

--
Zachary Burns
(407)590-4814
Aim - Zac256FL
Production Engineer
Zindagi Games
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20100419/c59b6987/attachment.html>


More information about the Python-ideas mailing list