Python 代码如何加入 hard warp?

2018-03-12 23:20:13 +08:00
 oIMOo

soft warp = 软行换。也就是只是显示的时候换行了而已,并不将换行保存到文件中。

hard warp = 硬换行。一次换,终生换,在哪里打开、用什么打开都是那里换。


平常用 Atom 写 C,使用 atom-beautify package 在保存的时候,都会在 length-weigth = 80 处换行。

现在想让 python 代码也在页宽为 80 的地方换行 (加 \ 并换行),同样使用 atom-beautify package, 测试了 autopep8 和 yapf 都没有这个功能。

Atom 自带的也只有 soft warp。

Sublime 改了 user setting, 同样是 soft warp。

google 一圈,没人有同样的强迫症,特来求助......

3260 次点击
所在节点    Python
24 条回复
snowolfy
2018-03-14 09:46:17 +08:00
@oIMOo
类似这种么?
```
foo = [
'a',
'b',
'c'
]
```

我觉得我更喜欢这种风格。而且好像看到的也多是这种风格。
估计看个人风格吧。我要求我这边的都不允许出现 `\` 来换行的。
glasslion
2018-03-14 09:53:51 +08:00
@oIMOo 打错了, 焕发 -> 换行

https://www.python.org/dev/peps/pep-0008/


The preferred way of wrapping long lines is by **using Python's implied line continuation inside parentheses, brackets and braces**. Long lines can be broken over multiple lines by wrapping expressions in parentheses. **These should be used in preference to using a backslash for line continuation.**

income = (gross_wages
+ taxable_interest
+ (dividends - qualified_dividends)
- ira_deduction
- student_loan_interest)

Google Python Style Guide:
https://google.github.io/styleguide/pyguide.html?showone=Line_length#Line_length

Do not use backslash line continuation.
Make use of Python's implicit line joining inside parentheses, brackets and braces. If necessary, you can add an extra pair of parentheses around an expression.
oIMOo
2018-03-14 09:56:02 +08:00
@snowolfy

类似于:

...split("overable'>")[

-1].split("blahblah...
oIMOo
2018-03-14 09:58:10 +08:00
@glasslion

我就是想遵照这个标准,本来想着从自动换行开始,一方面省事,一方面掌握规律。

我的 atom 貌似可以换行了,不过是正在输入中的代码,而不能对已有的代码文件进行换行处理。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/437451

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX