什么是好的Python格式样式 [英] What is a good Python format style

查看:72
本文介绍了什么是好的Python格式样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个小的python脚本,可以在用于测试的两个文件之间切换。

I've created a small python script to toggle between two files I'm using for testing.

我的问题是,什么是良好的Python格式样式以下代码:

My question is, what is a good Python format style for the following code:

import filecmp
import shutil

local = "local.txt"
remote = "remote.txt"
config_file = "C:\some\path\file.txt"

shutil.copyfile( remote if( filecmp.cmp(local, config_file ) ) else local, config_file  )

Or

shutil.copyfile( remote 
                     if( filecmp.cmp(local, config_file ) ) 
                     else local,
                 config_file  )

tocopy = remote if( filecmp.cmp( local, config_file ) ) else local 
shutil.copyfile( tocopy, config_file )

还是什么?

另外,在python中为多单词名称命名var的首选方法是 to_复制, tocopy, toCopy, ToCopy

Also, what is the preferred way to name var in python for many-word names, is it "to_copy", "tocopy", "toCopy", "ToCopy"

推荐答案

对于条件语句,我可能会选择:

For the conditional statement, I would probably go with:

if filecmp.cmp(local, config_file):
    shutil.copyfile(remote, config_file)
else:
    shutil.copyfile(local, config_file)

几乎不需要使用内联在这种情况下,如果 y c,因为周围的代码很简单。

There's little need to use the inline y if x else z in this case, since the surrounding code is simple enough.

这篇关于什么是好的Python格式样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆