python - PEP 8 - 通过添加空格来对齐参数
问题描述
我倾向于通过以下(非常规)方式添加空格来对齐我的代码。
def my_demo_function(input1, input2, input3):
print(input1 + input2 + input3)
my_demo_function("some long string", "some other string", "some final string")
my_demo_function("other string", "another string", "another other string")
my_demo_function("blah1", "blah2 string", "blah3" )
PEP-8似乎没有讨论这种(黑客?)对齐方式。有什么理由我不应该在我的代码中这样做吗?我可以设想这样一种情况,阅读它的人不会向右滚动并错过一些重要的东西。
解决方案
由于维护负担,通常会避免这种对齐方式,尤其是与版本控制系统(例如 git)结合使用时。
例如,假设您想稍后更改代码中的某些字符串,例如,"some long string"
更改为"some very long string"
. 然后你的代码变成
my_demo_function("some very long string", "some other string", "some final string")
my_demo_function("other string", "another string", "another other string")
my_demo_function("blah1", "blah2 string", "blah3" )
如果您不相应地更改其他行。
现在要保持对齐,您将在第二行和第三行添加更多空格,这可能会很麻烦(或不会,取决于您的文本编辑器)。
更重要的是,假设您使用的是 git。唯一“有意义”的更改是对第一行进行的。但是当你修改另外两行时,git 会记录这两行也发生了变化,尽管这种变化纯粹是一种装饰性的变化。当您稍后查看 git 历史记录时,这可能会使事情复杂化。
话虽如此,这一切都取决于你。如果你不使用 git,如果你预计代码将来不太可能被更改,或者如果你真的不在乎,那为什么不呢?:)
推荐阅读
- javascript - 按钮消失
- javascript - 将所有 Intel CPU 类型显示为 HTML 中的各种选项值
- python - 使用 python=3.6 和 tensoflow=1.15 加载本机 TensorFlow 运行时失败
- paypal - Paypal 沙盒订阅 Webhook 续订事件不起作用
- haskell - 没有因使用“liftIO”而产生 (MonadIO []) 的实例
- java - java.lang.IllegalStateException:键绑定 Ctrl+Shift+R 上的处理程序冲突
- python - 在matplotlib中以y轴单位测量网格的宽度
- vba - MS365 Word VBA Selection.Paste Word Table Run-time error: 4605, but not debug
- signalr - 如何从后台服务(如其他端口上的 API)调用 SignalR 客户端(javascript)?
- c# - 具有多个可选过滤器查询参数的 ASP.NET MVC GET 请求