jeudi 28 septembre 2017

Do Decorators in Python necessarily implement the Decorator Pattern?

In Python,

  1. Is it correct that decorators in Python can change the interface of the decorated function/class, e.g. change the signature of the decorated functions and methods of decorated class? For example:

    >>> def MyDecorator(func):
    ...     def NewFunc():
    ...         pass
    ...     return NewFunc
    ... 
    >>> @MyDecorator
    ... def MyFunc(a, b, c):
    ...     print(a,b,c)
    ...
    
    
  2. So decorators in Python are not necessarily used for implementing the decorator design pattern as described in the book "Design Pattern" by GoF?

  3. Is it a bad practice to write a decorator which change the interface of the decorated function/class?

Thanks.

Aucun commentaire:

Enregistrer un commentaire