As another answer has noted, this will not work because of how Python scopes variables.
Instead, therefore, what I suggest is that you move all these variables into a separate file, e.g. constants.py:
main.py
from functions import a
print(a())
constants.py
X = 1
functions.py
from constants import X
def a():
return X
Then, running import main prints 1.
As another answer has noted, this will not work because of how Python scopes variables.
Instead, therefore, what I suggest is that you move all these variables into a separate file, e.g. constants.py:
main.py
from functions import a
print(a())
constants.py
X = 1
functions.py
from constants import X
def a():
return X
Then, running import main prints 1.
There are no process-wide globals, only module-level globals. a uses functions.x, not the x in whatever global scope it is called from.
import functions
from functions import a, b, c
functions.x = 1
functions.y = 2
functions.z = 3
print(a())
print(b())
print(c())
Because Python is lexically scoped, each function keeps a reference to the global scope in which it was defined. Name lookups use that scope to resolve free variables.
Importing a function
Import inside functions
Importing the module doesn't waste anything; the module is always fully imported (into the sys.modules mapping), so whether you use import sys or from sys import argv makes no odds.
The only difference between the two statements is what name is bound; import sys binds the name sys to the module (so sys -> sys.modules['sys']), while from sys import argv binds a different name, argv, pointing straight at the attribute contained inside of the module (so argv -> sys.modules['sys'].argv). The rest of the sys module is still there, whether you use anything else from the module or not.
There is also no performance difference between the two approaches. Yes, sys.argv has to look up two things; it has to look up sys in your global namespace (finds the module), then look up the attribute argv. And yes, by using from sys import argv you can skip the attribute lookup, since you already have a direct reference to the attribute. But the import statement still has to do that work, it looks up the same attribute when importing, and you'll only ever need to use argv once. If you had to use argv thousands of times in a loop, it could perhaps make a difference, but in this specific case it really does not.
Hence, the choice between one or the other should be based solely on coding style.
In a large module, I'd certainly use import sys; code documentation matters, and using sys.argv somewhere in a large module makes it much clearer what you are referring to than just argv ever would.
If the only place you use argv is in a '__main__' block to call a main() function, by all means use from sys import argv if you feel happier about that:
if __name__ == '__main__':
from sys import argv
main(argv)
I'd still use import sys there myself. All things being equal (and they are, exactly, in terms of performance and number of characters used to write it), that is just easier on the eye for me.
If you are importing something else altogether, then perhaps performance comes into play. But only if you use a specific name in a module many times over, in a critical loop for example. But then creating a local name (within a function) is going to be faster still:
import somemodule
def somefunction():
localname = somemodule.somefunctionorother
while test:
# huge, critical loop
foo = localname(bar)
There are two reasons in favor of using import module rather than from module import function.
First is the namespace. Importing a function into the global namespace risks name collisions.
Second isn't that relevant to standard modules, but significant for you own modules, especially during development. It's the option to reload() a module. Consider this:
from module import func
...
reload(module)
# func still points to the old code
On the other hand
import module
...
reload(module)
# module.func points to the new code
As for speed...
we are importing only the function that is needed rather than import the whole module (which contains more useless functions which python will waste time importing them)
Whether you import a module or import a function from a module, Python will parse the whole module. Either way the module is imported. "Importing a function" is nothing more than binding the function to a name. In fact import module is less work for interpreter than from module import func.
Can anyone give me a good (technical) reason why I should not put import statements inside a function?
I use this a lot in python modules used by software that support python scripting. Especially in modules building custom menus to delay the imports just when the user run a function instead of importing tons of modules when the software launches. This helps me reduce the software startup time by a lot (in some case up to a minute if the software have a tons of custom tools and menus)
I always argue with colleagues about this and so far the only answer I get is “you are not suppose to do this”
Thanks