Zanurkuj w Pythonie/Metody specjalne: Różnice pomiędzy wersjami

Usunięta treść Dodana treść
Sasek (dyskusja | edycje)
tłumaczenie
Sasek (dyskusja | edycje)
Nie podano opisu zmian
Linia 38:
# Ta linia wygląda jak użycie słownika. Tylko, że <tt>f</tt> jest obiektem klasy próbującej udawać słownik a metoda <tt>__stitem__</tt> jest ważną częścią tej maskarady. Ta linia kodu powoduje właściwie wywołanie <tt>f.__setitem__("genre", 32)</tt>.
 
<tt>__setitem___setitem__</tt> jest metodą specjalną, ponieważ Python wywołuje ją za Ciebie, ale ciągle jest metodą klasy. Tak łatow, jak zdefiniowałeś ją w klasie <tt>UserDict</tt>, możesz ją przedefiniować w klasach pochodnych i zasłonić metodę klasy podstawowej. To pozwala Ci definiować klasy, które czasami zachowują się jak zwykły słownik ale potrafią coś więcej.
 
This concept is the basis of the entire framework you're studying in this chapter. Each file type can have a handler class that knows how to get metadata from a particular type of file. Once some attributes (like the file's name and location) are known, the handler class knows how to derive other attributes automatically. This is done by overriding the __setitem__ method, checking for particular keys, and adding additional processing when they are found.