Zanurkuj w Pythonie/Pakiety: Różnice pomiędzy wersjami
Usunięta treść Dodana treść
mNie podano opisu zmian |
tłumaczenie |
||
Linia 1:
{{WEdycji}}
{{Podświetl|py}}
== Pakiety ==
W rzeczywistość przetwarzanie dokumentu XML jest bardzo proste, wystarczy jedna linia kodu. Jednakże, zanim dojdziemy do tej linii kodu, będziemy musieli krótko omówić, czym są pakiety.
'''
>>> from xml.dom import minidom #(1)
>>> xmldoc = minidom.parse('~/diveintopython/common/py/kgp/binary.xml')
# Tę składnię nie widzieliśmy wcześniej. This is a syntax you haven't seen before.
Brzmi to skomplikowanie, ale tak naprawdę nie jest. Jeśli zobaczymy na konkretną implementacją, może nam to pomóc. Pakiet to niewiele więcej niż katalog z modułami, a zagnieżdżone pakiety to podkatalogami. The modules within a package (or a nested package) are still just .py files, like always, except that they're in a subdirectory instead of the main lib/ directory of your Python installation.
'''
Python21/ root Python installation (home of the executable)▼
|▼
+−−lib/ library directory (home of the standard library modules)▼
|▼
+−− xml/ xml package (really just a directory with other stuff in it)▼
|▼
+−−sax/ xml.sax package (again, just a directory)▼
|▼
+−−dom/ xml.dom package (contains minidom.py)▼
|▼
+−−parsers/ xml.parsers package (used internally)▼
▲ <span>Python21/ root Python installation (home of the executable)
▲ |
▲ +−−lib/ library directory (home of the standard library modules)
▲ |
▲ +−− xml/ xml package (really just a directory with other stuff in it)
▲ |
▲ +−−sax/ xml.sax package (again, just a directory)
▲ |
▲ +−−dom/ xml.dom package (contains minidom.py)
▲ |
▲ +−−parsers/ xml.parsers package (used internally)</span>
Dlatego kiedy powiesz <tt>from xml.dom import minidom</tt>, Python zrozumie to jako "znajdź w katalogu <tt>xml</tt> katalog <tt>dom</tt>, a następnie szukaj tutaj modułu <tt>minidom</tt> i zaimportuj go jako <tt>minidom</tt>". Lecz Python jest także mądrzejszy; nie tylko możesz zaimportować cały moduł zawarty wewnątrz pakietu, ale także możesz wybiórczo zaimportować wybrane klasy czy funkcje z modułu znajdującego się wewnątrz pakietu. Możesz także zaimportować sam pakiet jako moduł. Składnia będzie taka sama; Python wywnioskuje, co masz na myśli na podstawie struktury plików pakietu i automatycznie wykona poprawną czynność.
'''Przykład 9.7. Pakiety także są modułami'''
>>> from xml.dom import minidom #(1)▼
>>> minidom▼
<module 'xml.dom.minidom' from 'C:\Python21\lib\xml\dom\minidom.pyc'>▼
>>> minidom.Element▼
<class xml.dom.minidom.Element at 01095744>▼
>>> from xml.dom.minidom import Element #(2)▼
>>> Element▼
<class xml.dom.minidom.Element at 01095744>▼
>>> minidom.Element▼
<class xml.dom.minidom.Element at 01095744>▼
>>> from xml import dom #(3)▼
<module 'xml.dom' from 'C:\Python21\lib\xml\dom\__init__.pyc'>▼
>>> import xml #(4)▼
<module 'xml' from 'C:\Python21\lib\xml\__init__.pyc'>▼
#Here you're importing a module (minidom) from a nested package (xml.dom). The result is that minidom is imported into your namespace, and in order to reference classes within the minidom module (like Element), you need to preface them with the module name.▼
#Here you are importing a class (Element) from a module (minidom) from a nested package (xml.dom). The result is that Element is imported directly into your namespace. Note that this does not interfere with the previous import; the Element class can now be referenced in two ways (but it's all still the same class).▼
#Here you are importing the dom package (a nested package of xml) as a module in and of itself. Any level of a package can be treated as a module, as you'll see in a moment. It can even have its own attributes and methods, just the modules you've seen before.▼
#Here you are importing the root level xml package as a module.▼
>>> dom
>>> xml
▲# Here you're importing a module (minidom) from a nested package (xml.dom). The result is that minidom is imported into your namespace, and in order to reference classes within the minidom module (like Element), you need to preface them with the module name.
▲# Here you are importing a class (Element) from a module (minidom) from a nested package (xml.dom). The result is that Element is imported directly into your namespace. Note that this does not interfere with the previous import; the Element class can now be referenced in two ways (but it's all still the same class).
▲# Here you are importing the dom package (a nested package of xml) as a module in and of itself. Any level of a package can be treated as a module, as you'll see in a moment. It can even have its own attributes and methods, just the modules you've seen before.
▲# Here you are importing the root level xml package as a module.
So how can a package (which is just a directory on disk) be imported and treated as a module (which is always a file
|