我可以从python测试函数的名称中删除下划线吗
我有两个长名称函数:我可以从python测试函数的名称中删除下划线吗,python,Python,我有两个长名称函数: # ruby def test_write_method_raises_value_error # python def test_write_method_raises_value_error(self): 在ruby中,我可以重命名函数,如下所示: test 'write method should raise value error' do # rest of function Python中有类似的构造吗?如果为了测试目的只需要缩短函数名,那么很容易做到
# ruby
def test_write_method_raises_value_error
# python
def test_write_method_raises_value_error(self):
在ruby中,我可以重命名函数,如下所示:
test 'write method should raise value error' do
# rest of function
Python中有类似的构造吗?如果为了测试目的只需要缩短函数名,那么很容易做到
python中的一切都是对象,即使是函数。您可以将其分配给变量,实际上它只是一个标签
例如:
def very_very_long_function_name_that_we_want_to_test(argument):
pass
# test:
testable = very_very_long_function_name_that_we_want_to_test
# This will call our "long function"
testable('test')
但是,根据Python命名约束,这个函数的新名称应该是正确的,例如没有空格您可以将方法保存在变量中。您可以删除它,但pep8表示它不是Python风格。请检查这个。Python不会给出错误,但不是它的风格。可能的重复不会重命名函数;我不太了解Ruby,但这看起来像是对调用test
返回的每个项目调用块的语法。您可以在Python中模拟它(在语法限制范围内),但由于它不是惯用语言,我怀疑您是否愿意这样做。