你能在一条线上调用多种方法吗?

Omi*_*ron 5 python python-2.7 python-3.x

例如,我想:

texta = text.lower()
textacopy1 = texta.replace(string.punctuation, ' ')
textacopy2 = textacopy1.split(' ')
Run Code Online (Sandbox Code Playgroud)

有没有更简洁的方法来做这个而不必分配多个变量?

如果2.7和3.x之间存在差异,我更倾向于3.x解释.

Sha*_*mad 14

result = text.lower().replace(string.punctuation, ' ').split(' ')
Run Code Online (Sandbox Code Playgroud)

伟大的蟒蛇带来了很大的责任:不要滥用这个功能!

PEP 8中规范的规范最大行长度为80个字符,分割方法链的规范方法是在点处开始新行:

result = text.lower().replace(string.punctuation, ' ')
        .split(' ')
Run Code Online (Sandbox Code Playgroud)

  • 点触感很好 - 不知道 (2认同)