相关疑难解决方法(0)

如何编写内联if语句用于打印?

我需要在布尔变量设置为时才打印一些东西True.所以,看后这个,我试图用一个简单的例子:

>>> a = 100
>>> b = True
>>> print a if b
  File "<stdin>", line 1
    print a if b
             ^
SyntaxError: invalid syntax  
Run Code Online (Sandbox Code Playgroud)

如果我写的话也一样print a if b==True.

我在这里错过了什么?

python if-statement inline

342
推荐指数
12
解决办法
57万
查看次数

Python风格

简单的初学者问题:

我已经创建了一个小的python脚本来切换我用于测试的两个文件.

我的问题是,对于以下代码,什么是一个好的python格式样式:

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  )
Run Code Online (Sandbox Code Playgroud)

要么

shutil.copyfile( remote 
                     if( filecmp.cmp(local, config_file ) ) 
                     else local,
                 config_file  )
Run Code Online (Sandbox Code Playgroud)

要么

tocopy = remote if( filecmp.cmp( local, config_file ) ) else local 
shutil.copyfile( tocopy, config_file )
Run Code Online (Sandbox Code Playgroud)

或者是什么?

另外,对于多字名称在python中命名var的优先方法是什么,是"to_copy","tocopy","toCopy","ToCopy"

谢谢.

python coding-style

3
推荐指数
4
解决办法
1289
查看次数

标签 统计

python ×2

coding-style ×1

if-statement ×1

inline ×1