python - PEP8 在断言中破坏长字符串

标签 python

<分区>

我有这行代码:

assert 0 <= j <= self.n, "First edge needs to be between 0 and {}".format(self.n)

我希望 pep8 快乐,但我不明白如何打破这条线。我尝试在逗号后断开并得到无效语法。我试过用额外的“”打破字符串,如 How to break long string lines for PEP8 compliance? . PEP8 很高兴,但断言只产生了消息的前半部分。

中断长断言字符串的正确方法是什么?

最佳答案

使用括号:

assert 0 <= j <= self.n, ("First edge needs to be "
                          "between 0 and {}".format(self.n))

或者:

assert 0 <= j <= self.n, ("First edge needs to be between 0 and {}"
                          .format(self.n))

或者使用 format 函数的括号:

assert 0 <= j <= self.n, "First edge needs to be between 0 and {}".format(
    self.n)

关于python - PEP8 在断言中破坏长字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38165051/

相关文章:

python - Pandas 不根据列值进行过滤

python - pdb 重复下一条命令 N 次

python - 使用 txt 文件作为输入创建 RDF 文件

python - 查找变量位置的函数?

python - 将 DateTimeIndex 与月年合并

python - C函数传递一个指针和一个长度,Python回调需要创建一个数组并为其赋值

python - 如何对时间值列表进行排序?

python - Point() 接受 0 个位置子模式(给定 2 个)

python - 从 Pandas 数据框中获取某个日期或之前的值的计数

python - 你见过的 Django 中最糟糕的做法