python - 如何格式化符合 PEP8 的 python 断言语句?

标签 python assert pep8

如何格式化符合 PEP8 的长断言语句?请忽略我的例子的人为性质。

def afunc(some_param_name):
    assert isinstance(some_param_name, SomeClassName), 'some_param_name must be an instance of SomeClassName, silly goose!'

不能用括号括起来,因为这会改变 assert 语句的行为,因为它是关键字,而不是内置函数。

最佳答案

请务必记住,PEP8 只是一个指南,even states that there are times when the rules should be broken .

But most importantly: know when to be inconsistent -- sometimes the style guide just doesn't apply.

考虑到这一点,我可能会用旧式续行来写这个:

def afunc(some_param_name):
    assert isinstance(some_param_name, SomeClassName), \ 
           'some_param_name must be an instance of SomeClassName, silly goose!'

如果您(或您的 linter)不满意,您可以随时这样做:

def afunc(some_param_name):
    assert isinstance(some_param_name, SomeClassName), ( 
           'some_param_name must be an instance of SomeClassName, silly goose!')

甚至:

def afunc(some_param_name):
    assert isinstance(some_param_name, SomeClassName), ( 
           'some_param_name must be an instance of SomeClassName, '
           'silly goose!')

关于python - 如何格式化符合 PEP8 的 python 断言语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16065482/

相关文章:

python - 在 Pyqt4 Python 中从 QtableWidget 中的 QlineEdit 检索值

linux - Pthread互斥断言错误

java - 正则表达式模式未断言 true

python - Python 的 PEP8 行长度限制是否适用于注释?

python CSV : field containing quotation mark at the beginning

python - 如何使用多个索引从 NumPy 数组中获取值

python - 在 OS X Lion 上强制 Python 为 32 位

debugging - 断言具有副作用的函数的返回值的 Rust 方法是什么?

python - Pandas .loc 和 PEP8

python - flake8 不报告大小写混合的函数名称