Set, Mark, and Apply Decoration

Sean Ross sross at
Thu Aug 12 01:56:00 CEST 2004

Set, Mark, and Apply Decoration

Provide a means to set the decoration that will be applied to all
marked functions (and maybe classes) within a scope (module, class,
or function).

For example purposes, I will use a decor object that would presumably
be made available in each scope (via locals, perhaps). I'll say that
it has (atleast[*]) a method set() with the following signature:

    def set(self, *decorators, **attributes):

This method could be used to set/reset the decor, 0 to n times, within a
given scope. The decor of one scope would only be applicable within itself
and, perhaps, within its nested scopes. In this way, you could define a
region(s) wherein the same decoration(s) could be applied to marked
functions - there would be no need to repeat the decoration declarations
for functions that share the same decor.

The functions to be decorated (using the current decor) would be marked.
This demarcation could be done by a symbol or by a keyword. E.g.,

'<MARK> def foo():'  OR  'def <MARK> foo():'  OR  'def foo() <MARK>:'

Where <MARK> can be replaced by whatever is deemed the least objectionable
symbol or keyword. For example purposes, I'll use 'def *foo():'.

I'll give an example, now, showing this proposal side by side with the
current decoration scheme:

# current scheme        # proposed scheme
class A(object):            class A(object):
    # static methods           # static methods
    @staticmethod              decor.set(staticmethod)
    def f():                             def *f():
        pass                                pass
    @staticmethod              def *g():
    def g():                                 pass
        pass                             ...
        ...                                  ...
    # undecorated                # undecorated
    def h():                             def h():
        pass                                pass

    # class methods             # class methods
    @classmethod               decor.set(classmethod)
    def i():                              def *i():
        pass                                pass
    @classmethod               def *j():
    def j():                                  pass
        pass                            ...
    ...                                     ...
    # garish                          # garish
    D = compose(A,B,C,    decor.set(A,B,C,
      funcattrs(d='d',                 d='d',e='e',f='f'
        e='e',f='f'))                   def *k():
    @D                                     pass
    def k():                            def *l():
        pass                               pass
    @D                                 ...
    def l():                             ...
       pass                            ...

NOTE: That garish one is *much* worse if you don't use function composition.

You might want a way to clear the decor, or a way to add or remove
decorations without resetting the entire decor, or a way to ask what
the current decor is, if any.

More information about the Python-list mailing list