Why would the validation 'if not set(sys.argv)' be needed?

62 views Asked by At

I'm trying to understand the first part of the next validation. Can I safely assume that the expression 'if not set(sys.argv)' is always False? Code is from the Ironic Project.

if not set(sys.argv) & valid_commands:

According to the docs, if the command is run with '-c' option, argv[0] is set to the string '-c'. Which is still a non-empty set. Example:

python -c '
import sys
print(sys.argv[0])
if not set(sys.argv):
    print("empty")
else:
    print("non-empty")
'
2

There are 2 answers

0
user2357112 On BEST ANSWER

You're reading it wrong. Python's not has very low precedence, below that of & (which isn't logical AND).

if not set(sys.argv) & valid_commands:

should be read as

if not (set(sys.argv) & valid_commands):

which is testing whether sys.argv contains any elements of the set valid_commands.

0
NPE On

There is no "first part" here. The

if not set(sys.argv) & valid_commands:

is equivalent to

if not (set(sys.argv) & valid_commands):