17.12.2012 Views

Programmation PYTHON - Zenk - Security - Repository

Programmation PYTHON - Zenk - Security - Repository

Programmation PYTHON - Zenk - Security - Repository

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

sorted(['c', 'a', 'b'], cmp=cmp, key=key)<br />

key('c') = -99<br />

key('a') = -97<br />

key('b') = -98<br />

cmp(-97, -99) = -1<br />

cmp(-98, -97) = 1<br />

cmp(-98, -99) = -1<br />

cmp(-98, -97) = 1<br />

['a', 'b', 'c']<br />

staticmethod : staticmethod(fonction) -> méthode statique<br />

Les primitives<br />

CHAPITRE 6<br />

Transforme une fonction en une méthode statique. Une méthode statique est une<br />

méthode qui n’est pas dépendante de l’instance de classe. Le premier paramètre<br />

implicite qui contient cet objet n’est donc pas fourni et toutes les instances de la<br />

classe ou la classe elle-même pourront utiliser cette méthode de la même manière et<br />

obtenir les mêmes résultats.<br />

Méthode statique<br />

>>> class MyClass(object):<br />

... def static_method():<br />

... print("je suis universelle")<br />

... static_method = staticmethod(static_method)<br />

...<br />

>>> MyClass.static_method()<br />

je suis universelle<br />

>>> instance = MyClass()<br />

>>> instance.static_method()<br />

je suis universelle<br />

Cette écriture peut être remplacée par un appel par decorator.<br />

Écriture abrégée par decorator<br />

>>> class MyClass(object):<br />

... @staticmethod<br />

... def static_method():<br />

... print("je suis universelle")<br />

...<br />

>>> MyClass.static_method()<br />

je suis universelle<br />

>>> instance = MyClass()<br />

>>> instance.static_method()<br />

je suis universelle<br />

181

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!