pep鼓励还是不鼓励链式方法(方法级联)?

2024-03-29 09:04:02 发布

您现在位置:Python中文网/ 问答频道 /正文

受这个post的启发,我假设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上没有找到相关的文档

你知道吗?你知道吗


Tags: 方法证明mostcallpost内置list级联
1条回答
网友
1楼 · 发布于 2024-03-29 09:04:02

pep8让您决定如何最好地布局代码。贯穿始终的关键主题是您的代码应该清晰易读。 您提供的示例不起作用。append不返回任何内容。你知道吗

下面是一个字符串示例:

x = "This"
x = x.strip().replace("T","t")
print (x)

在下面的布局中,哪个更容易阅读:

x = "This"
x = (x
     .strip()
     .replace("T","t")
     )
print (x)

相关问题 更多 >