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.

Le nouveau Method Resolution Order<br />

Structuration du code<br />

CHAPITRE 5<br />

La mécanique de recherche des attributs s’appelle le Method Resolution Order<br />

(MRO) et utilise un algorithme qui parcourt l’arborescence des classes en profondeur<br />

puis de gauche à droite.<br />

Cette mécanique change avec l’introduction d’object comme type de base commun<br />

aux types fournis dans Python. En effet, l’ancien algorithme ne pouvait plus<br />

répondre à tous les cas d’héritages multiples introduits par l’insertion de object dans<br />

l’héritage de certains types. Ainsi, l’héritage en « diamant » provoquait avec l’algorithme<br />

précédent un fonctionnement illogique.<br />

Utilisation de __mro__<br />

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

... brand = ''<br />

... def print_brand(self):<br />

... print(self.brand)<br />

...<br />

>>> class TelevisionSatellite(Television):<br />

... channels = []<br />

... def list_channels(self):<br />

... return self. channels<br />

...<br />

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

... def play_dvd(self):<br />

... pass<br />

...<br />

>>> class DVDWriter(DVDPlayer):<br />

... def write_dvd(self):<br />

... pass<br />

...<br />

>>> class SuperTVDVDCombo(TelevisionSatellite, DVDWriter):<br />

... pass<br />

...<br />

>>> dir(SuperTVDVDCombo)<br />

['__class__', '__delattr__', '__dict__', '__doc__', '__format__',<br />

'__getattribute__', '__hash__', '__init__', '__module__', '__new__',<br />

'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',<br />

'__str__', '__subclasshook__', '__weakref__', 'brand', 'channels',<br />

'list_channels', 'play_dvd', 'print_brand', 'write_dvd']<br />

>>> SuperTVDVDCombo.__mro__<br />

(, , , , , )<br />

127

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

Saved successfully!

Ooh no, something went wrong!