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 [2013/12/03 20:57] – root | python:decorateurs [2017/03/22 21:06] (Version actuelle) – [Built-in decorator] root | ||
---|---|---|---|
Ligne 23: | Ligne 23: | ||
def x(self): | def x(self): | ||
del self._x | 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) | ||
</ | </ | ||
Ligne 34: | Ligne 87: | ||
# Post-traitement | # Post-traitement | ||
return response | return response | ||
- | wrapper.__doc__ = func.__doc__ | + | |
- | | + | wrapper.__name__ = func.__name__ |
return wrapper | return wrapper | ||
</ | </ |
python/decorateurs.1386104234.txt.gz · Dernière modification : 2013/12/03 20:57 de root