We use all the time python's import mechanism to import modules and variables and other stuff..but, is there anything that works as export? like:
we import stuff from a module:
from abc import *
so can we export like?:
to xyz export *
or export a,b,c to program.py
I know this question isn't a typical type of question to be asked here..but just in curiosity..I checked over the python console and there is nothing that exists as 'export'..maybe it exists with some different name..?
First, import the module you want to export stuff into, so you have a reference to it. Then assign the things you want to export as attributes of the module:
# to xyz export a, b, c
import xyz
xyz.a = a
xyz.b = b
xyz.c = c
To do a wildcard export, you can use a loop:
# to xyz export *
exports = [(k, v) for (k, v) in globals().iteritems() if not k.startswith("_")]
import xyz
for k, v in exports: setattr(xyz, k, v)
(Note that we gather the list of objects to be exported before importing the module, so that we can avoid exporting a reference to the module we've just imported into itself.)
This is basically a form of monkey-patching. It has its time and place. Of course, for it to work, the module that does the "exporting" must itself be executed; simply importing the module that will be patched won't magically realize that some other code somewhere is going to patch it.
来源:https://stackoverflow.com/questions/14163532/is-there-anything-like-python-export