简体   繁体   English

为什么这个python代码有效?

[英]Why does this python code work?

# this code I don't understand 
def cls():                       #if statement freaks me out
    os.system('cls' if os.name=='nt' else 'clear')

I understand that it works but not WHY it works. 我知道它有效,但不是为什么它有效。 After several hours of perusing google, python docs, and stackoverflow, I am none the wiser. 经过几个小时的浏览谷歌,python文档和stackoverflow,我不是更聪明。 I have been unable to find anything explaining that manner of using an IF statement in a function call like this. 我一直无法找到任何解释在这样的函数调用中使用IF语句的方式。 I have run it under 2.7 and 3.5 so it does not appear to be specific to a particular version. 我已经在2.7和3.5下运行它,所以它似乎并不特定于特定版本。 I have seen similar stuff done with for loops sort of get that it might be a standard way of doing things. 我已经看到类似的东西完成for循环排序得到它可能是一种标准的做事方式。 I kind of like it myself. 我有点像自己。 Just don't understand how it works. 只是不明白它是如何工作的。

I am confused about how the IF statement is evaluated within system call and I am especially confused about the syntax of the IF statement. 我对如何在系统调用中评估IF语句感到困惑,我对IF语句的语法特别感到困惑。 I have not seen that syntax in any of the documentation I have read. 我在我读过的任何文档中都没有看到这种语法。

Python is completely new to me. Python对我来说是全新的。 So forgive me if this is bonehead simple. 如果这是简单的傻瓜,请原谅我。 But I don't get it. 但我不明白。

If you are more familiar with C-like languages and their ternary operator, Python's a if b else c is similar to b ? a : c 如果您更熟悉类C语言及其三元运算符,那么Python的a if b else cb ? a : c类似b ? a : c b ? a : c . b ? a : c Therefore, the code above says: 因此,上面的代码说:

if we are on NT/Windows
  then use `cls`
else (for Linux, etc) use `clear`

The result is then passed to the os.system() command to perform the OS-specific operation. 然后将结果传递给os.system()命令以执行特定于操作系统的操作。

Here is some tutorial information on this subject . 以下是有关此主题的一些教程信息

A if C else B

This first evaluates C; 这首先评估C; if it is true, A is evaluated to give the result, otherwise, B is evaluated to give the result. 如果为真,则评估A以给出结果,否则,评估B以给出结果。

This shortcut conditional expression syntax was added since Python 2.5 Check it here 自从Python 2.5 在此处检查后,添加了此快捷方式条件表达式语法

There's similar syntax in other languages, taking Java for example: min = (a<b)? a: b 在其他语言中有类似的语法,以Java为例: min = (a<b)? a: b min = (a<b)? a: b which checks whether a is smaller than b, it returns a if a is smaller, otherwise returns b. min = (a<b)? a: b检查a是否小于b,如果a小则返回a,否则返回b。 BTW, it is call ternary operator in java. 顺便说一句,它是java中的调用三元运算符

In your case: 在你的情况下:

'cls' if os.name=='nt' else 'clear' it checks whether os.name equals to string nt , if it is, it returns cls , otherwise it returns clear 'cls' if os.name=='nt' else 'clear'它检查os.name是否等于字符串nt ,如果是,则返回cls ,否则返回clear

this inline if - else is the python version of ternary operator. 这个内联if - else是三元运算符的python版本。

in languages like C/C++/Java/JavaScript you would write 用C / C ++ / Java / JavaScript等语言编写

a = b > c ? 10 : 20

in python you would write the same as 在python中你会写同样的

a = 10 if b > c else 20

you can use the same construct to pass parameters to functions. 您可以使用相同的构造将参数传递给函数。

in C/C++ 在C / C ++中

foo(b > c ? 10 : 20)

in python 在python中

foo(10 if b > c else 20)

The if expression is not executed inside of the system call. if表达式不在系统调用内执行。 It is executed before the system call. 它在系统调用之前执行。 Your code is equivalent to 你的代码相当于

command = 'cls' if os.name=='nt' else 'clear'
os.system(command)

The if expression itself is only a short form for the if statement if表达式本身只是if语句的简短形式

if os.name=='nt':
    command = 'cls'
else:
    command = 'clear'
os.system(command)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM