Python风格 [英] Python style
本文介绍了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 )
或
shutil.copyfile( remote
if( filecmp.cmp(local, config_file ) )
else local,
config_file )
p>
Or
tocopy = remote if( filecmp.cmp( local, config_file ) ) else local
shutil.copyfile( tocopy, config_file )
还是什么?
是对于许多单词名称在python中命名var的优先方式,是to_copy,tocopy,toCopy,ToCopy
Also, what is the preffered way to name var in python for many-word names, is it "to_copy", "tocopy", "toCopy", "ToCopy"
/ p>
Thanks.
推荐答案
对于条件语句,我可能会用:
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)
几乎不需要使用inline y if x else z
在这种情况下,因为周围的代码很简单。
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屋!
查看全文