问题描述
如何格式化符合PEP8的长断言语句?请忽略我的示例的人为设计性质.
How does one format a long assert statement that complies with PEP8? Please ignore the contrived nature of my example.
def afunc(some_param_name):
assert isinstance(some_param_name, SomeClassName), 'some_param_name must be an instance of SomeClassName, silly goose!'
不能将其包装在括号中,因为这会更改assert语句的行为,因为它是关键字,而不是内置函数.
One cannot wrap it in parenthesis, because that changes the behavior of the assert statement since it is a keyword, not a builtin function.
推荐答案
重要的是要记住,PEP8只是一个准则,.
It's important to remember that PEP8 is only a guideline and even states that there are times when the rules should be broken.
请牢记这一点,我可能会使用旧样式的行继续符来编写此代码:
With that in mind, I would probably write this with old style line continuation:
def afunc(some_param_name):
assert isinstance(some_param_name, SomeClassName), \
'some_param_name must be an instance of SomeClassName, silly goose!'
如果这对您(或您的短毛猫)来说不合适,您可以随时这样做:
If that doesn't sit well with you (or your linter), you can always do:
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!')
这篇关于如何格式化符合PEP8的python断言语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!