简体   繁体   English

Python…sys.argv中的“ If false”:这是什么意思?

[英]Python… “If false” in sys.argv: What does it mean?

I've seen this line in a python script, but I don't understand it. 我已经在python脚本中看到了这一行,但我不明白。 What does it mean? 这是什么意思?

if False or '--epson' in sys.argv:

Python doesn't have multi-line comments, so a hack some programmers use is to put an if False: in front of a block of code they want to disable for testing. Python没有多行注释,因此,一些程序员使用的一种技巧是if False:的代码块前放置一个if False: To re-enable the block, they'd change it back to "True." 要重新启用该块,他们会将其更改回“ True”。

In your case, this was probably already an if statement, so they set it up like this so they could disable the entire block: 在您的情况下,这可能已经是一个if语句,因此他们像这样设置它,以便可以禁用整个块:

print "Hello world!"
if False: # or "--epson" in sys.argv
    print "Do something fancy with the --epson flag."
    print "Disable me if you're testing!"
    print "And me."
    print "Whoops, me too. Wow, that's a lot of single-line comments."
print "That's it, goodbye."

This is generally a bad practice because it isn't immediately obvious that this block is disabled. 这通常是一种不好的做法,因为禁用此块并不立即显而易见。 It's preferable to use an editor that makes it easy to comment/uncomment multiple lines at once. 最好使用可以轻松注释/取消注释多行的编辑器。

if A or B

in this case A = False ... which will always be False 在这种情况下,A = False ...始终为False

and B = "--epson" in sys.argv which will be True IFF --epson is in sys.argv 并且"--epson" in sys.argv B = "--epson" in sys.argv ,它将是True IFF --syson中的epson

...you could rewrite this as ...您可以将其重写为

if "--epson" in sys.argv:

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

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