我正在创建一个模块,我将在主脚本中导入。在称为“颜色”的模块中,有一个函数:“info()”。在调用函数之前强制使用模块名称,在Python中
颜色模块:
def info(function=None):
print("\ncolors\n Info\n")
我的问题是,我还可以创建一个名为“信息()”功能,在主脚本。这会不会是一个问题,只要我输入的颜色模块为:
colors.info()
如果我来代替:
import colors
如果是的话,我会通过写作调用模块中的功能导入模块为:
from colors import *
我有两个函数叫做完全相同。
主要脚本:
from colors import *
def info(): # A
print("Main script's function")
info() # A
colors.info() # Am I able to force use of the module name before calling the
# function, if I import the module as in this script? Can this
# be done from the module, and not from the main script? As said,
# force use of module name, as "colors.info()", shall only apply
# when the module is being imported with "from colors import *".
编辑1
我为什么要导入模块,这样的原因,是因为在它的全局变量:
bwc0 = (0, 0, 0)
bwc1 = (1, 1, 1)
bwc2 = (2, 2, 2)
# All the way to 255
后一些答案,我会尝试将这些添加到类中,并尽可能将该类导入为*。 我也有一些功能,我也想输入为*。我不确定如何导入它们。我想可能很简单。
def redc(value):
return value, 0, 0
def greenc(value):
return 0, value, value
感谢所有帮助
使用通配符在进口被认为这将是一个反模式 –
一个改变坏主意。我只能想象所有的问题在“为什么我无法调用函数info()',尽管我明确地**在脚本中声明了它?”。如果有人导入*([这是坏的](http://stackoverflow.com/questions/2386714/why-is-import-bad),并定义他们自己的'info'函数,如果忽略这个函数将是非常可怕的作为另一个被导入的模块的副作用。 – kazemakase
我会''将颜色导入为co',所以你同时拥有'info()'和'co.info()':不再更长,并且从长远来看可以节省头痛。 – farsil