Something about python3 modules and namespaces
Assignments do not copy data — they just bind names to objects.
Module
A module can contain executable statements as well as function definitions. These statements are intended to initialize the module. They are executed only the first time the module name is encountered in an import statement.from fibo import *
import all the namespaces in the module fibofrom fibo import fib,fib1
import the namespace fib and fib1 in the moduleimport fibo
import the module fibo, and should call the member functions in this way fibo.fib(1000)
PATH
- current dir
- sys.path
dir(fibo)
[‘name‘, ‘fib’, ‘fib2’] list all the names defined in the module
PACKAGES
An example hierarchy from the official documentation.
|
|
from sound.effects import echo
echofilter(input, output, delay=0.7, atten=4)
add the ‘all‘ in the init.py__all__ = ["echo", "surround", "reverse"]
Namespaces
example in the official documentation
|
|
|
|
- the innermost scope, which is searched first, contains the local names
- the scopes of any enclosing functions, which are searched starting with the nearest enclosing scope, contains non-local, but also non-global names
- the next-to-last scope contains the current module’s global names
- the outermost scope (searched last) is the namespace containing built-in names
近期评论