16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 10 ■ BATTERIES INCLUDED 205<br />

If each module could be imported several times, you would end up with a problem here. The module<br />

clientdb would import billing, which again imports clientdb, which . . . you get the picture. You get an<br />

endless loop of imports (endless recursion, remember?). However, because nothing happens the second time<br />

you import the module, the loop is broken.<br />

If you insist on reloading your module, you can use the built-in function reload. It takes a single argument<br />

(the module you want <strong>to</strong> reload) and returns the reloaded module. This may be useful if you have made<br />

changes <strong>to</strong> your module and want those changes reflected in your program while it is running. To reload the<br />

simple hello module (containing only a print statement), you would use the following:<br />

>>> hello = reload(hello)<br />

Hello, world!<br />

Here I assume that hello has already been imported (once). By assigning the result of reload <strong>to</strong> hello,<br />

I have replaced the previous version with the reloaded one. As you can see from the printed greeting, I am<br />

really importing the module here.<br />

Note that if you’ve created an object using a given module and you then reload that module, your object<br />

will not be re-created. If you want your object <strong>to</strong> be based on the reloaded module, you will have <strong>to</strong> create it anew.<br />

Modules Are Used <strong>to</strong> Define Things<br />

So modules are executed the first time they are imported in<strong>to</strong> your program. That seems sort of<br />

useful—but not very. What makes them worthwhile is that they (just like classes) keep their<br />

scope around afterward. That means that any class or function you define, and any variable<br />

you assign a value <strong>to</strong>, become attributes of the module. This may seem complicated, but in<br />

practice it is very simple. Let’s say you have written a module like the one in Listing 10-2 and<br />

s<strong>to</strong>red it in a file called hello2.py. Also assume that you’ve put it in a place where the <strong>Python</strong><br />

interpreter can find it, either using the sys.path trick from the previous section, or the more<br />

conventional methods from the section “Making Your Modules Available,” which follows.<br />

Listing 10-2. A Simple Module Containing a Function<br />

# hello2.py<br />

def hello():<br />

print "Hello, world!"<br />

You can then import it like this:<br />

>>> import hello2<br />

The module is then executed, which means that the function hello is defined in the scope<br />

of the module, which means that you can access the function like this:<br />

>>> hello2.hello()<br />

Hello, world!<br />

Any name defined in the global scope of the module will be available in the same manner.

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

Saved successfully!

Ooh no, something went wrong!