Skip to main content

Packages are modules, too

>>> from xml.dom import minidom
>>> minidom

>>> minidom.Element

>>> from xml.dom.minidom import Element
>>> Element

>>> minidom.Element

>>> from xml import dom
>>> dom

>>> import xml
>>> 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
on disk)? The answer is the magical __init__.py file. You see, packages are not simply directories; they are
directories with a specific file, __init__.py, inside. This file defines the attributes and methods of the package.
For instance, xml.dom contains a Node class, which is defined in xml/dom/__init__.py. When you import a
package as a module (like dom from xml), you're really importing its __init__.py file.
A package is a directory with the special __init__.py file in it. The __init__.py file defines the attributes
and methods of the package. It doesn't need to define anything; it can just be an empty file, but it has to exist. But if
__init__.py doesn't exist, the directory is just a directory, not a package, and it can't be imported or contain
modules or nested packages.
So why bother with packages? Well, they provide a way to logically group related modules. Instead of having an xml
package with sax and dom packages inside, the authors could have chosen to put all the sax functionality in
xmlsax.py and all the dom functionality in xmldom.py, or even put all of it in a single module. But that would

have been unwieldy (as of this writing, the XML package has over 3000 lines of code) and difficult to manage
(separate source files mean multiple people can work on different areas simultaneously).
If you ever find yourself writing a large subsystem in Python (or, more likely, when you realize that your small
subsystem has grown into a large one), invest some time designing a good package architecture. It's one of the many
things Python is good at, so take advantage of it.

Comments

Popular posts from this blog

CKA Simulator Kubernetes 1.22

  https://killer.sh Pre Setup Once you've gained access to your terminal it might be wise to spend ~1 minute to setup your environment. You could set these: alias k = kubectl                         # will already be pre-configured export do = "--dry-run=client -o yaml"     # k get pod x $do export now = "--force --grace-period 0"   # k delete pod x $now Vim To make vim use 2 spaces for a tab edit ~/.vimrc to contain: set tabstop=2 set expandtab set shiftwidth=2 More setup suggestions are in the tips section .     Question 1 | Contexts Task weight: 1%   You have access to multiple clusters from your main terminal through kubectl contexts. Write all those context names into /opt/course/1/contexts . Next write a command to display the current context into /opt/course/1/context_default_kubectl.sh , the command should use kubectl . Finally write a second command doing the same thing into ...

OWASP Top 10 Threats and Mitigations Exam - Single Select

Last updated 4 Aug 11 Course Title: OWASP Top 10 Threats and Mitigation Exam Questions - Single Select 1) Which of the following consequences is most likely to occur due to an injection attack? Spoofing Cross-site request forgery Denial of service   Correct Insecure direct object references 2) Your application is created using a language that does not support a clear distinction between code and data. Which vulnerability is most likely to occur in your application? Injection   Correct Insecure direct object references Failure to restrict URL access Insufficient transport layer protection 3) Which of the following scenarios is most likely to cause an injection attack? Unvalidated input is embedded in an instruction stream.   Correct Unvalidated input can be distinguished from valid instructions. A Web application does not validate a client’s access to a resource. A Web action performs an operation on behalf of the user without checkin...