
Specific Statements
|
49
The from Statement
from [package.]* module import name [,name]*
from [package.]* module import *
from [package.]* module import name as othername
from [package.]* module import (name1, name2,
name3, name4)
The from statement imports variable names from a module so
that you can use those names later without the need to qual-
ify them with their module name. The
from mod import * for-
mat copies all names assigned at the top level of the module,
except names with a single leading underscore or names not
listed in the module’s
__all__ list-of-strings attribute (if
defined).
If used, the
as clause creates a name synonym. If used,
package import paths work the same as in import statements
(e.g.,
from dir1.dir2.mod import X), but the package path
needs to be listed only in the
from itself. Due to new scoping
rules, the
* format generates warnings in Version 2.2 if it
appears nested in a function or class.
As of Python 2.4, the names being imported from a module
can be enclosed in parentheses to span multiple lines with-
out backslashes.
The
from statement is also used to enable future (but still
experimental) language additions, with
from __future__
import featurename
. This format must appear only at the top
of a module file (preceded only by an optional doc string).
The class Statement
class name [ ( super [, super]* ) ]:
suite
The class statement makes new class objects, which are fac-
tories