Decorator Base Class: Needs improvement.

Bengt Richter bokr at oz.net
Wed Apr 6 03:34:15 EDT 2005


On 5 Apr 2005 19:28:55 -0700, "El Pitonero" <pitonero at gmail.com> wrote:

>Scott David Daniels wrote:
>> Ron_Adam wrote:
>> > ...
>>
>>      def tweakdoc(name):
>>          def decorator(function):
>> 	    function.__doc__ = 'Tweak(%s) %r' % (name, function.__doc__)
>> 	    return function
>>          return decorator
>>
>> What is confusing us about what you write is that you are referring
>to
>> tweakdoc as a decorator, when it is a function returning a decorator.
>
>"Decorator factory" would be a shorter name for "a function returning a
>decorator".
>
True, but tweakdoc doesn't have to be a function, so IMO we need a better
name for the @-line, unless you want to use many various specific names
like factory. E.g.,

(Preliminary definition):

 >>> class Deco(object):
 ...     class __metaclass__(type):
 ...         def __getattribute__(cls, name):
 ...             if hasattr(type(cls), name): return type.__getattribute__(cls, name)
 ...             def decorator(f):
 ...                 f.func_name = '%s_%s'%(f.func_name, name)
 ...                 return f
 ...             return decorator
 ...
 >>> deco = Deco

Ok, now what do you call this @-line? Freaky decorator factory invocation?
Decoratoriferous expression? ;-) Lexical decoration?
Decorator{-producing,-generating, ??} expression ?

Decorator expression for short? vs the decorator callable produced.

 >>> @deco.one
 ... def foo(): pass
 ...
 >>> foo
 <function foo_one at 0x02EE8E2C>
 >>> @deco.three
 ... @deco.two
 ... def bar():pass
 ...
 >>> bar
 <function bar_two_three at 0x02EE8E9C>

Regards,
Bengt Richter



More information about the Python-list mailing list