15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

It is possible to import specific module elements into your own module. By this, we really mean<br />

importing specific names from the module into the current namespace. For this purpose, we can use the<br />

from-import statement, whose syntax is:<br />

from module import name1[, name2[,... nameN]]<br />

12.4.3. Multi-Line Import<br />

The multi-line import feature was added in <strong>Python</strong> 2.4 specifically for long from-import statements.<br />

When importing many attributes from the same module, import lines of code tend to get long and wrap,<br />

requiring a NEWLINE-escaping backslash. Here is the example imported (pun intended) directly from<br />

PEP 328:<br />

from Tkinter import Tk, Frame, Button, Entry, Canvas, \<br />

Text, LEFT, DISABLED, NORMAL, RIDGE, END<br />

Your other option is to have multiple from-import statements:<br />

from Tkinter import Tk, Frame, Button, Entry, Canvas, Text<br />

from Tkinter import LEFT, DISABLED, NORMAL, RIDGE, END<br />

We are also trying to stem usage on the unfavored from Tkinter import * (see the <strong>Core</strong> Style sidebar in<br />

Section 12.5.3). Instead, programmers should be free to use <strong>Python</strong>'s standard grouping mechanism<br />

(parentheses) to create a more reasonable multi-line import statement:<br />

from Tkinter import (Tk, Frame, Button, Entry, Canvas,<br />

Text, LEFT, DISABLED, NORMAL, RIDGE, END)<br />

You can find out more about multi-line imports in the documentation or in PEP 328.<br />

12.4.4. Extended Import Statement (as)<br />

There are times when you are importing either a module or module attribute with a name that you are<br />

already using in your application, or perhaps it is a name that you do not want to use. Maybe the name<br />

is too long to type everywhere, or more subjectively, perhaps it is a name that you just plain do not like.<br />

This had been a fairly common request from <strong>Python</strong> programmers: the ability to import modules and<br />

module attributes into a program using names other than their original given names. One common

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

Saved successfully!

Ooh no, something went wrong!