python:decorateurs
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
python:decorateurs [2012/04/22 13:19] – créée root | python:decorateurs [2017/03/22 21:06] (Version actuelle) – [Built-in decorator] root | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
====== Décorateurs Python ====== | ====== Décorateurs Python ====== | ||
+ | |||
+ | Source : http:// | ||
+ | |||
+ | ===== Built-in decorator ===== | ||
+ | * **@property** : transforme une fonction en propriété (en gros transforme une fonctionne comme une variable) | ||
+ | |||
+ | <code python> | ||
+ | class C(object): | ||
+ | def __init__(self): | ||
+ | self._x = None | ||
+ | |||
+ | @property | ||
+ | def x(self): | ||
+ | """ | ||
+ | return self._x | ||
+ | |||
+ | @x.setter | ||
+ | def x(self, value): | ||
+ | self._x = value | ||
+ | |||
+ | @x.deleter | ||
+ | def x(self): | ||
+ | del self._x | ||
+ | </ | ||
+ | |||
+ | <code python> | ||
+ | # | ||
+ | # -*- coding: utf-8 -*- | ||
+ | |||
+ | class myclass(object): | ||
+ | x = 0 | ||
+ | |||
+ | def __init__(self): | ||
+ | print(' | ||
+ | self.x = 0 | ||
+ | |||
+ | # Methode classique. Ne peux pas être appelée sans être instanciée | ||
+ | # myclass().foo(1) | ||
+ | # myclass.foo(1) >>> | ||
+ | def foo(self, x): | ||
+ | print(" | ||
+ | self.x+=x | ||
+ | |||
+ | # Pas besoin d' | ||
+ | # myclass().class_foo(1) >>> | ||
+ | # myclass.class_foo(1) | ||
+ | @classmethod | ||
+ | def class_foo(cls, | ||
+ | print(" | ||
+ | cls.x+=x | ||
+ | cls.static_foo(1) | ||
+ | |||
+ | # Pas besoin d' | ||
+ | # myclass().static_foo(1) >>> | ||
+ | # myclass.static_foo(1) | ||
+ | @staticmethod | ||
+ | def static_foo(x): | ||
+ | print(" | ||
+ | x+=1 | ||
+ | |||
+ | if __name__ == ' | ||
+ | a=myclass() | ||
+ | print(' | ||
+ | a.foo(1) | ||
+ | print(' | ||
+ | a.class_foo(1) | ||
+ | print(' | ||
+ | a.static_foo(1) | ||
+ | print(' | ||
+ | print(' | ||
+ | try: | ||
+ | myclass.foo(1) | ||
+ | except Exception as e: | ||
+ | print(e) | ||
+ | myclass.class_foo(1) | ||
+ | myclass.static_foo(1) | ||
+ | </ | ||
===== Sans arguments ===== | ===== Sans arguments ===== | ||
- | < | + | < |
def decorate(func): | def decorate(func): | ||
def wrapper(*args, | def wrapper(*args, | ||
Ligne 10: | Ligne 87: | ||
# Post-traitement | # Post-traitement | ||
return response | return response | ||
+ | wrapper.__doc__ = func.__doc__ | ||
+ | wrapper.__name__ = func.__name__ | ||
return wrapper | return wrapper | ||
</ | </ | ||
Exemple : | Exemple : | ||
- | < | + | < |
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
Ligne 42: | Ligne 121: | ||
===== Avec arguments ===== | ===== Avec arguments ===== | ||
- | < | + | < |
def decorate(arg1, | def decorate(arg1, | ||
def decorated(func): | def decorated(func): | ||
Ligne 52: | Ligne 131: | ||
return wrapper | return wrapper | ||
return decorated | return decorated | ||
+ | </ | ||
+ | |||
+ | <code python> | ||
+ | def decorate(arg1=' | ||
+ | def decorated(func): | ||
+ | def wrapper(*args, | ||
+ | # Pré-traitement | ||
+ | response = func(*args, **kwargs) | ||
+ | # Post-traitement | ||
+ | return response | ||
+ | return wrapper | ||
+ | return decorated | ||
+ | |||
+ | @decorate(arg1=' | ||
+ | def foobar(): | ||
+ | pass | ||
+ | </ | ||
+ | |||
+ | <code python> | ||
+ | # MAUVAIS | ||
+ | @decorate | ||
+ | def foobar(): | ||
+ | pass | ||
+ | |||
+ | # BON | ||
+ | @decorate() | ||
+ | def foobar(): | ||
+ | pass | ||
</ | </ | ||
Exemple : | Exemple : | ||
- | < | + | < |
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
python/decorateurs.1335100770.txt.gz · Dernière modification : 2012/04/22 13:19 de root