Importing Select Symbols from a Module
- The
fromstatement is used to load specific defintions within a module into the current namespace - In other words,
fromis very similar toimport - However,
fromonly refers to objects defined in a module - Then, it places these references into the current namespace
>>> from spam import foo
>>> foo()
'foo'
>>> spam.foo()
NameError: spam- We can access multiple objects using:
>>> from spam import (foo,
... bar,
... Spam)- We can load all definitions into the module namespace:
>>> from spam import *- Modules can control which names can be imported using
__all__
>>> # module: spam.py
>>> __all__ = ['bar', 'Spam'] # names exported using
... # from spam import *References
Previous
Next