如何将 python 文件导入到一个模块的多个文件中,而不使用相同的变量?
How to import a python file to multiple files of a module with them not using the same variables?
我有一个 python 文件,假设它是 common.py
在此,我有一些功能,以及功能所需的变量:
TAG = '[TESTTAG]'
def myprint(obj):
print(f'{TAG} {obj}')
def set_tag(out_tag):
global TAG
TAG = out_tag
我希望能够使用 myprint()
函数而不必每次都传递 TAG
参数。
为了设置这个 TAG
,我编写了 set_tag()
函数
我还有 2 个文件,我想在其中使用 myprint()
函数,但标签不同:
use1.py
from common import *
set_tag('[USE1TAG]')
def testusage():
myprint('First sentence')
myprint('Second sentence')
if __name__ == '__main__':
testusage()
use2.py
from common import *
set_tag('[USE2TAG]')
def testusage2():
myprint('Third sentence')
myprint('Fourth sentence')
if __name__ == '__main__':
testusage2()
当我单独 运行 它们时,它们会产生预期的结果。
但问题是我想将这两个文件导入到一个最终文件中,并多次使用它们的函数,每次都使用我之前在它们的源文件中设置的 TAG
。这样,最新导入的文件将 TAG
更改为 [USE2TAG]
,并将继续使用。
combine.py
from use1 import testusage
from use2 import testusage2
if __name__ == '__main__':
testusage()
testusage2()
testusage()
testusage2()
输出:
[USE2TAG] First sentence
[USE2TAG] Second sentence
[USE2TAG] Third sentence
[USE2TAG] Fourth sentence
[USE2TAG] First sentence
[USE2TAG] Second sentence
[USE2TAG] Third sentence
[USE2TAG] Fourth sentence
预期结果:
[USE1TAG] First sentence
[USE1TAG] Second sentence
[USE2TAG] Third sentence
[USE2TAG] Fourth sentence
[USE1TAG] First sentence
[USE1TAG] Second sentence
[USE2TAG] Third sentence
[USE2TAG] Fourth sentence
问题在于它们对同一个 TAG
变量进行操作。
我知道,我每次都可以将 TAG
传递给 myprint()
函数,但我认为必须有一种方法可以不使用它。
我知道我可以为 use1.py
和 use2.py
定义 myprint()
函数,但我宁愿将它作为“服务”导入,所以我不每次都必须附加它。
有没有办法在不传递TAG
参数的情况下在多个文件中使用myprint()
函数?
感谢您的回答!
实现此目的的一种方法是拥有一个 returns 正确配置的打印功能,然后您可以使用该功能。为此,您可以使用 lambda。例如:
def _myprint(tag, obj):
print(f'[{tag}] {obj}')
def get_myprint(tag):
return lambda obj, tag=tag: _myprint(tag, obj)
然后在使用它的地方你可以做类似的事情:
print_a = get_myprint('a')
print_b = get_myprint('b')
print_a('test a')
print_b('test b')
给出
[a] test a
[b] test b
你应该看看 functools.partial
,这对这种事情非常有帮助。在您定义 myprint 的文件中,定义了一个通用函数:
def generic_myprint(tag, obj):
print(f'{TAG} {obj}')
然后,在导入 myprint 的文件中,包含以下代码:
from functools import partial
from common import generic_myprint
myprint = partial(generic_myprint, tag='[USE1TAG]')
显然,为每个需要不同值的文件替换 tag
参数。
functools.partial
接受一个有很多参数的函数,returns 一个新的 partial
对象,其行为与原始函数完全相同,但有一个或多个参数"预加载”作为默认值。在这种情况下,myprint
现在在调用时只需要 obj
参数。 generic_myprint
接受 2 个参数,但 myprint
只接受 1 个。
在我看来,这比使用 lambda
函数更像 pythonic,但你的里程可能会有所不同!
我有一个 python 文件,假设它是 common.py
在此,我有一些功能,以及功能所需的变量:
TAG = '[TESTTAG]'
def myprint(obj):
print(f'{TAG} {obj}')
def set_tag(out_tag):
global TAG
TAG = out_tag
我希望能够使用 myprint()
函数而不必每次都传递 TAG
参数。
为了设置这个 TAG
,我编写了 set_tag()
函数
我还有 2 个文件,我想在其中使用 myprint()
函数,但标签不同:
use1.py
from common import *
set_tag('[USE1TAG]')
def testusage():
myprint('First sentence')
myprint('Second sentence')
if __name__ == '__main__':
testusage()
use2.py
from common import *
set_tag('[USE2TAG]')
def testusage2():
myprint('Third sentence')
myprint('Fourth sentence')
if __name__ == '__main__':
testusage2()
当我单独 运行 它们时,它们会产生预期的结果。
但问题是我想将这两个文件导入到一个最终文件中,并多次使用它们的函数,每次都使用我之前在它们的源文件中设置的 TAG
。这样,最新导入的文件将 TAG
更改为 [USE2TAG]
,并将继续使用。
combine.py
from use1 import testusage
from use2 import testusage2
if __name__ == '__main__':
testusage()
testusage2()
testusage()
testusage2()
输出:
[USE2TAG] First sentence
[USE2TAG] Second sentence
[USE2TAG] Third sentence
[USE2TAG] Fourth sentence
[USE2TAG] First sentence
[USE2TAG] Second sentence
[USE2TAG] Third sentence
[USE2TAG] Fourth sentence
预期结果:
[USE1TAG] First sentence
[USE1TAG] Second sentence
[USE2TAG] Third sentence
[USE2TAG] Fourth sentence
[USE1TAG] First sentence
[USE1TAG] Second sentence
[USE2TAG] Third sentence
[USE2TAG] Fourth sentence
问题在于它们对同一个 TAG
变量进行操作。
我知道,我每次都可以将 TAG
传递给 myprint()
函数,但我认为必须有一种方法可以不使用它。
我知道我可以为 use1.py
和 use2.py
定义 myprint()
函数,但我宁愿将它作为“服务”导入,所以我不每次都必须附加它。
有没有办法在不传递TAG
参数的情况下在多个文件中使用myprint()
函数?
感谢您的回答!
实现此目的的一种方法是拥有一个 returns 正确配置的打印功能,然后您可以使用该功能。为此,您可以使用 lambda。例如:
def _myprint(tag, obj):
print(f'[{tag}] {obj}')
def get_myprint(tag):
return lambda obj, tag=tag: _myprint(tag, obj)
然后在使用它的地方你可以做类似的事情:
print_a = get_myprint('a')
print_b = get_myprint('b')
print_a('test a')
print_b('test b')
给出
[a] test a
[b] test b
你应该看看 functools.partial
,这对这种事情非常有帮助。在您定义 myprint 的文件中,定义了一个通用函数:
def generic_myprint(tag, obj):
print(f'{TAG} {obj}')
然后,在导入 myprint 的文件中,包含以下代码:
from functools import partial
from common import generic_myprint
myprint = partial(generic_myprint, tag='[USE1TAG]')
显然,为每个需要不同值的文件替换 tag
参数。
functools.partial
接受一个有很多参数的函数,returns 一个新的 partial
对象,其行为与原始函数完全相同,但有一个或多个参数"预加载”作为默认值。在这种情况下,myprint
现在在调用时只需要 obj
参数。 generic_myprint
接受 2 个参数,但 myprint
只接受 1 个。
在我看来,这比使用 lambda
函数更像 pythonic,但你的里程可能会有所不同!