Consider the following code:
考虑下面的代码:
#main.py
From toolsmodule import *
database = "foo"
#toolsmodule
database = "mydatabase"
As it seems, this creates one variable in each module with different content. How can I modify the variable inside toolsmodule from main? The following does not work:
看起来,这在每个模块中创建了一个具有不同内容的变量。如何从main修改toolsmodule内部的变量?以下不可行:
toolsmodule.database = "foo"
3 个解决方案
#1
14
Sounds like yet another of the multitude of good reasons not to use from toolsmodule import *
.
这听起来像是众多不使用工具结构导入的好理由之一*。
If you just do import toolsmodule
, then you can do toolsmodule.database = 'foo'
, and everything is wonderful.
如果您只做导入工具烟雾,那么您可以做工具烟雾。数据库= 'foo',一切都很棒。
#2
7
Pythons variable names are just labels on variables. When you import *
all those labels are local and when you then set the database, you just replace the local variable, not the one in toolsmodule. Hence, do this:
python变量名只是变量的标签。当您导入*时,所有的标签都是本地的,当您设置数据库时,您只需替换本地变量,而不是toolsmodule中的那个。因此,这样做:
toolsmodule.py:
toolsmodule.py:
database = "original"
def printdatabase():
print "Database is", database
And then run:
然后运行:
import toolsmodule
toolsmodule.database = "newdatabase"
toolsmodule.printdatabase()
The output is
输出是
Database is newdatabase
Note that if you then from ANOTHER module ALSO did an import *
the change is not reflected.
注意,如果您从另一个模块也做了导入*更改没有反映出来。
In short: NEVER use from x import *
. I don't know why all newbies persist in doing this despite all documentation I know of says that it's a bad idea.
简而言之:永远不要从x导入*。我不知道为什么所有的新手都坚持这么做,尽管我知道所有的文件都说这是个坏主意。
#3
1
Why don't you do it like that:
你为什么不这样做呢:
import toolsmodule
toolsmodule.database = "foo"
from toolsmodule import * #bad idea, but let's say you have to..
print database #outputs foo
#1
14
Sounds like yet another of the multitude of good reasons not to use from toolsmodule import *
.
这听起来像是众多不使用工具结构导入的好理由之一*。
If you just do import toolsmodule
, then you can do toolsmodule.database = 'foo'
, and everything is wonderful.
如果您只做导入工具烟雾,那么您可以做工具烟雾。数据库= 'foo',一切都很棒。
#2
7
Pythons variable names are just labels on variables. When you import *
all those labels are local and when you then set the database, you just replace the local variable, not the one in toolsmodule. Hence, do this:
python变量名只是变量的标签。当您导入*时,所有的标签都是本地的,当您设置数据库时,您只需替换本地变量,而不是toolsmodule中的那个。因此,这样做:
toolsmodule.py:
toolsmodule.py:
database = "original"
def printdatabase():
print "Database is", database
And then run:
然后运行:
import toolsmodule
toolsmodule.database = "newdatabase"
toolsmodule.printdatabase()
The output is
输出是
Database is newdatabase
Note that if you then from ANOTHER module ALSO did an import *
the change is not reflected.
注意,如果您从另一个模块也做了导入*更改没有反映出来。
In short: NEVER use from x import *
. I don't know why all newbies persist in doing this despite all documentation I know of says that it's a bad idea.
简而言之:永远不要从x导入*。我不知道为什么所有的新手都坚持这么做,尽管我知道所有的文件都说这是个坏主意。
#3
1
Why don't you do it like that:
你为什么不这样做呢:
import toolsmodule
toolsmodule.database = "foo"
from toolsmodule import * #bad idea, but let's say you have to..
print database #outputs foo