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 中有类似的构造吗?

最佳答案

如果您只是为了测试目的而需要缩短函数名称,则可以轻松完成。

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命名限制,这个函数的新名称应该是正确的,例如没有空格

关于python - 我可以从 python 测试函数的名称中删除下划线吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26588154/

相关文章:

python - Google 通讯录插入中的性别更改为 "Directory server"

python - Celery 和 Redis 后端的问题

带有 SWI-Prolog 的 Python 接口(interface)

python - 为什么所有对象都使用相同的坐标填充?

python - 如何在python中将字符串转换为json?

python - 如何让virtualenv Django服务器工作?//根据命令提示符 'missing attribute'

python - 信息检索中的语言模型

python - 根据 pandas 中另一列的值添加一列的值

python - 在我的批处理文件重命名和移动脚本中遇到 Python os.rename() 问题

python - Jupyter 笔记本不保存 : '_xsrf' argument missing from post