What is a good Python format style

 ̄綄美尐妖づ 提交于 2019-12-20 02:15:17

问题


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

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  )

Or

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

Or what?

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


回答1:


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)

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




回答2:


From the Python Style Guide:

With regard to listing out a compound expression:

Compound statements (multiple statements on the same line) are generally discouraged.

Yes:

if foo == 'blah':
    do_blah_thing()
do_one()
do_two()
do_three()

Or for the code you supplied, Greg's example is a good one:

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

Rather not:

if foo == 'blah': do_blah_thing()
do_one(); do_two(); do_three()

Method Names and Instance Variables

Use the function naming rules: lowercase with words separated by underscores as necessary to improve readability.

Update: Per Oscar's request, also listed how his code would look in this fashion.




回答3:


The third option looks the most natural to me, although your use of spaces in side parentheses and superfluous parentheses contradict the Python style guide.

That guide also answers the to_copy question, but I would probably use clearer names altogether.

I would write it as:

import filecmp
import shutil

local = "local.txt"
remote = "remote.txt"

destination = r"C:\some\path\file.txt"
source = remote if filecmp.cmp(local, destination) else local

shutil.copyfile(source, destination)



回答4:


The most common naming I've seen is underscode separated words, to_copy.

As for the format style, I've seen no such agreement. I find

source = remote if filecmp.cmp(local, config_file) else local

shutil.copyfile(source, config_file)

to be the clearest among your options.

And seeing that everyone prefers to split the if I'd, at the very least, encapsulate the copyfile call in case you someday wish to change it:

def copy_to(source, destination):
    shutil.copyfile(source,destination)

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



回答5:


You might find this useful; PEP 8 -- Style Guide for Python Code




回答6:


What about:

import filecmp
import shutil

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


if filecmp.cmp( local, config_file):
    to_copy = remote
else:
    to_copy = local


shutil.copyfile( to_copy, config_file  )

yikes, this open id screen name looks terrible.



来源:https://stackoverflow.com/questions/331767/what-is-a-good-python-format-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!