Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there anything like Python export?

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..?

like image 822
khan Avatar asked Jan 04 '13 19:01

khan


People also ask

Is there an export in Python?

Exporting a model to a Python script can help you learn how tools and environments are used in Python. To export a model to Python, click the Export button on the ModelBuilder ribbon and choose one of the following options: Export To Python File. Send To Python window.

What does export mean in Python?

export is a command that you give directly to the shell (e.g. bash ), to tell it to add or modify one of its environment variables. You can't change your shell's environment from a child process (such as Python), it's just not possible.


1 Answers

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.

like image 119
kindall Avatar answered Nov 03 '22 02:11

kindall