Python 如何在每次类调用时初始化全局变量?
我有一个模块a,如下所示:Python 如何在每次类调用时初始化全局变量?,python,Python,我有一个模块a,如下所示: import unittest2 def check_function(): # performs actions and returns True or False return smth CHECK = check_function() class A(unittest2.TestCase): @unittest2.skipIf(CHECK, "skip message 1") def test_1(self):
import unittest2
def check_function():
# performs actions and returns True or False
return smth
CHECK = check_function()
class A(unittest2.TestCase):
@unittest2.skipIf(CHECK, "skip message 1")
def test_1(self):
# test check
@unittest2.skipIf(CHECK, "skip message 2")
def test_2(self):
# test check
模块A正在由另一个称为B的模块导入。
全局变量检查何时初始化?进口?在类实例化时
我需要在每次调用类A时设置CHECK变量。我怎样才能做到这一点
编辑:
我尝试了以下方法(这可能是我正在寻找的),但在setUpClass中根本没有设置CHECK(无论CHECK_函数()返回什么,它都保持为False)
您知道如何在每次调用测试时设置一次检查吗
编辑:
check_function()确实被调用过一次,但我不明白为什么unittest2.skipIf没有“看到”setUpClass中设置的值,而是坚持声明中设置的假值
解决方案:
代码的最终框架如下所示:
import unittest2
def check_function():
# performs checks and returns True or False
return smth
class A(unittest2.TestCase):
CHECK = check_function()
@unittest2.skipIf(CHECK, "skip message 1")
def test_1(self):
# do tests
self.assertTrue(True)
@unittest2.skipIf(CHECK, "skip message 1")
def test_2(self):
# do tests
self.assertTrue(True)
首次导入模块时,
检查
变量初始化
参见示例。我有一个模块mymodule.py
,它包含:
print "i am mymodule"
还有一些anothermodule.py
:
print "first"
import mymodule
print "second"
import mymodule
print "third"
def get_a():
print 'init a'
return 42
def get_b():
print 'init b'
return 20
a = get_a()
b = get_b()
当我运行另一个module.py时,我得到:
first
i am mymodule
second
third
python中的Invitialization变量与print
和
将在解释器首次进入模块时逐行执行
更清晰的示例。
mymodule.py
:
print "first"
import mymodule
print "second"
import mymodule
print "third"
def get_a():
print 'init a'
return 42
def get_b():
print 'init b'
return 20
a = get_a()
b = get_b()
anothermodule.py
from mymodule import a
print "a =", a
print "b =", b
结果:
init a
init b
a = 42
Traceback (most recent call last):
File "anothermodule.py", line 3, in <module>
print b
NameError: name 'b' is not defined
inita
初始b
a=42
回溯(最近一次呼叫最后一次):
文件“anothermodule.py”,第3行,在
打印b
名称错误:未定义名称“b”
您的第二种方法看起来像是一个起点。但是你遇到了时间问题
一方面,您希望在应用装饰器时显示该值(这很早),另一方面,您在调用setUpClass()
时设置该值。那可能已经很晚了
在我看来,唯一的选择似乎是这样做
class A(unittest2.TestCase):
CHECK = check_function()
@unittest2.skipIf(CHECK, "skip message 1")
def test_1(self):
# test check
@unittest2.skipIf(CHECK, "skip message 2")
def test_2(self):
# test check
其中,检查
的初始化时间早于使用时间。如何:
import unittest2
def check_function():
# performs actions and returns True or False
return smth
CHECK = None
class A(unittest2.TestCase):
def __init__(self,*args,**kwargs):
CHECK=check_funtion
super(A,self).__init__(*args,**kwargs)
@unittest2.skipIf(CHECK, "skip message 1")
def test_1(self):
# test check
@unittest2.skipIf(CHECK, "skip message 2")
def test_2(self):
# test check
该变量在解释该行后立即可用。一个程序通常是从上到下运行的。你为什么不使用调试器一步一步地检查代码并亲自查看呢?另外,作为术语,您不“调用”类(除非它有
\uuu调用\uuu
方法)。您可以调用方法、函数、lambda和。如果为True,则不能调用任何类。但是在这种情况下,A是实例化的,并且由于它的所有方法都被调用(比如说使用nose),我们不能将这个术语扩展到整个类吗?@Alex Nope,除非你定义了它的意思,在这一点上,你可以使用定义,而不是滥用定义良好的术语。那么您希望什么时候设置CHECK变量呢?每次调用测试时?是的,完全正确。我想在每次调用测试时都设置检查变量。所以全局变量在多个模块中是“已知”和全局的,对吗?它们只对写入它们的模块是“全局的”,不会在其他任何地方自动变为全局的。@Alex实际上没有全局变量,只有模块级变量。但是,由于任何模块都可以导入任何其他模块,从而可以访问该模块中的内容,这实际上与您描述的一样,只是在多个模块中使用相同的名称不会导致问题——赞扬名称空间。我忘了提到,在声明它们的模块导入到其他模块后,它们会变得“已知”.如果我按照你说的做,并在类外声明它,那么当调用unittest2.skipIf时,它会保留该值;如果我按照你说的做,并且没有在类外声明它,我会得到以下错误:NameError:name'CHECK'没有定义。无论如何都不行。@Alex因为时间问题更新了答案。非常感谢!这确实是一个时间问题。看起来在调用decorators之前,check_function()只被调用一次。