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

80 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 best solutions below

0
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
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):