python:decorateurs
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| python:decorateurs [2012/04/22 13:24] – 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 ===== | ||
| - | Source : http:// | + | < |
| - | + | ||
| - | < | + | |
| def decorate(arg1, | def decorate(arg1, | ||
| def decorated(func): | def decorated(func): | ||
| Ligne 56: | Ligne 133: | ||
| </ | </ | ||
| - | < | + | < |
| def decorate(arg1=' | def decorate(arg1=' | ||
| def decorated(func): | def decorated(func): | ||
| Ligne 72: | Ligne 149: | ||
| </ | </ | ||
| - | < | + | < |
| # MAUVAIS | # MAUVAIS | ||
| @decorate | @decorate | ||
| Ligne 82: | Ligne 159: | ||
| def foobar(): | def foobar(): | ||
| pass | pass | ||
| - | < | + | </code> |
| Exemple : | Exemple : | ||
| - | < | + | < |
| # -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
python/decorateurs.1335101059.txt.gz · Dernière modification : de root
