如何格式化符合PEP8的python断言语句? [英] How to format a python assert statement that complies with PEP8?

查看:75
本文介绍了如何格式化符合PEP8的python断言语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何格式化符合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.

但最重要的是:知道何时需要不一致-有时样式指南就不适用.

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

请牢记这一点,我可能会使用旧样式的行继续符来编写此代码:

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断言语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆