-1

inspired by this post, I am assuming that PEP8 is discouraging chained method (method cascading).

built-ins are the proof.

>>> 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'

but i didn't find related doc by search on pep

any idea?

1 Answers1

1

PEP 8 leaves serval things up to you to decide how best to layout your code. The key theme throughout is that your code should be clear and readable. The example you provide doesn’t work as .append returns nothing.

Here is an example with strings:

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

Which might be easier to read in the following layout:

x = "This"
x = (x
     .strip()
     .replace("T","t")
     )
print (x)
CodeCupboard
  • 1,507
  • 3
  • 17
  • 26