Search code examples
pythoncoding-style

What is a good Python format style


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"


Solution

  • 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.