Zanurkuj w Pythonie/Klasa opakowująca UserDict: Różnice pomiędzy wersjami

Usunięta treść Dodana treść
Linia 6:
W <tt>ActivePython IDE on Windows</tt> możesz szybko otwierać dowolny moduł znajdujący się w twojej ścieżce do bibliotek <!--your library path--> wybierając ''File->Locate... (Ctrl-L)''.}}
 
'''ExamplePrzykład 5.9. Definicja klasy <tt>UserDict</tt>'''
 
 
Linia 16:
# Klasa <tt>UserDict</tt> jest klasą bazową, nie dziedziczy nic z innych klas.
# Metodę <tt>__init__</tt> nadpisaliśmy w klasie <tt>FileInfo</tt>. Zauważmy, że lista argumentów w klasie przodka jest różna niż w klasie potomka. Jest ok. Każda podklasa może mieć własny zbiór argumentów, pod warunkiem, że metody przodka będą wywoływane z poprawnymi argumentami. Tutaj klasa przodka posiada możliwość zdefiniowania początkowych wartości (dzięki przekazaniu słownika w argumencie <tt>dict</tt>), jednak której klasa <tt>FileInfo</tt> nie wykorzystuje.
# Python wspiera właściwości (zwane polami w Javie i PowerBuilderze). Właściwości to kawałki danych przechowywane w konkretnej instancji klasy. W tym przypadku, każda instancja klasy <tt>UserDict</tt> będzie posiadać właściwość <tt>data</tt>. By odwołać się do tego pola z kodu spoza klasy, dodajemy z przodu nazwę instancji - instancja.data, w ten sam sposób jak odwołujemy się do funkcji poprzez nazwę modułu w jakim się znajduje. By odwołać się do właściwości z wnętrza klasy, używamy <tt>self</tt>. Zazwyczaj wszystkie właściwości są inicjalizowane sensownymi wartościami w metodzie <tt>__init__</tt>. Jednak nie jest to wymagane, gdyż właściwości tak jak zmienne lokalne są tworzone gdy pierwszy raz przypisuje się do nich wartość.
# Python supports data attributes (called “instance variables” in Java and Powerbuilder, and “member variables” in C++). Data attributes are pieces of data held by a specific instance of a class. In this case, each instance of UserDict will have a data attribute data. To reference this attribute from code outside the class, you qualify it with the instance name, instance.data, in the same way that you qualify a function with its module name. To reference a data attribute from within the class, you use self as the qualifier. By convention, all data attributes are initialized to reasonable values in the __init__ method. However, this is not required, since data attributes, like local variables, spring into existence when they are first assigned a value.
# The update method is a dictionary duplicator: it copies all the keys and values from one dictionary to another. This does not clear the target dictionary first; if the target dictionary already has some keys, the ones from the source dictionary will be overwritten, but others will be left untouched. Think of update as a merge function, not a copy function.
# This is a syntax you may not have seen before (I haven't used it in the examples in this book). It's an if statement, but instead of having an indented block starting on the next line, there is just a single statement on the same line, after the colon. This is perfectly legal syntax, which is just a shortcut you can use when you have only one statement in a block. (It's like specifying a single statement without braces in C++.) You can use this syntax, or you can have indented code on subsequent lines, but you can't do both for the same block.