12.5. Importing Module Attributes

It is possible to import specific module elements into your own module. By this, we really mean importing specific names from the module into the current namespace. For this purpose, we can use the from-import statement, whose syntax is:

						from
						module
						import
						name1[, name2[, … nameN]]

12.5.1. Names Imported into Current Namespace

Calling from-import brings the name into the current namespace, meaning that you do not use the attribute/dotted notation to access the module identifier. For example, to access a variable named var in module module that was imported with:

							from
							module
							import
							var
						

we would use var by itself. There is no need to reference the module since you just imported.

It is also possible to import all the names from the module into the current namespace using the following from-import statement:

							from
							module
							import *
						

CORE STYLE:Restrict your use of “from module import *

In practice, using from module import * is considered poor style because it “pollutes” the current namespace and has the potential of overriding names in the current namespace; however, it is extremely convenient if a module has many variables which are often accessed, or if the module has a very long name.

We recommend using this form in only two situations. The first is where the target module has many attributes that would make it inconvenient to type in the module name over and over again. Two prime examples of this are the Tkinter (Python/Tk) and NumPy (Numeric Python) modules, and perhaps the socket module. The other place where it is acceptable to use from module import * is within the interactive interpreter, to save on the amount of typing.


12.5.2. Names Imported into Importer's Scope

Another side effect of importing only names from other modules is that the names are now part of the scope of the importing module.This means that changes to the variable affect only the local copy and not the original in the imported module's namespace. In other words, the binding is now local rather than across namespaces.

Below, we present the code to two modules: an importer, impter.py, and an importee, imptee.py. Currently, imptr.py uses the from-import statement which creates only local bindings.

#############
# imptee.py #
#############
foo = 'abc'
def show():
    print 'foo from imptee:', foo


#############
# impter.py #
#############
from imptee import foo, show
show()
foo = 123
print 'foo from impter:', foo
show()

Upon running the importer, we discover that the importee's view of its foo variable has not changed even though we modified it in the importer.

foo from imptee: abc
foo from impter: 123
foo from imptee: abc

The only solution is to use import and fully-qualified identifier names using the attribute/dotted notation.

#############
# impter.py #
#############
import imptee
imptee.show()
imptee.foo = 123
print 'foo from impter:', imptee.foo
imptee.show()

Once we make the update and change our references accordingly, we now have achieved the desired effect.

foo from imptee: abc
foo from impter: 123
foo from imptee: 123

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset