pep 鼓励还是不鼓励链式方法(方法级联)?
Is chained method (method cascading) encouraged or discouraged by pep?
受此 启发,我假设 PEP8 不鼓励链式方法(方法级联)。
内置函数就是证明。
>>> x = list()
>>> x.append(1).append(2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'append'
但我在 pep
上搜索没有找到相关文档
有什么想法吗?
PEP 8 将一些事情留给您来决定如何最好地布局您的代码。贯穿始终的关键主题是您的代码应该清晰易读。
您提供的示例不起作用,因为 .append returns nothing.
这是一个字符串示例:
x = "This"
x = x.strip().replace("T","t")
print (x)
以下布局可能更容易阅读:
x = "This"
x = (x
.strip()
.replace("T","t")
)
print (x)
受此
内置函数就是证明。
>>> x = list()
>>> x.append(1).append(2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'append'
但我在 pep
上搜索没有找到相关文档有什么想法吗?
PEP 8 将一些事情留给您来决定如何最好地布局您的代码。贯穿始终的关键主题是您的代码应该清晰易读。 您提供的示例不起作用,因为 .append returns nothing.
这是一个字符串示例:
x = "This"
x = x.strip().replace("T","t")
print (x)
以下布局可能更容易阅读:
x = "This"
x = (x
.strip()
.replace("T","t")
)
print (x)