I have this python coded statement:
is_headless = ["--headless"] if sys.argv[0].find('console.py') != -1 else [""]
- In what way does the blank between
["--headless"]andifcontrol the code line? - How and would
"--headless"ever be an element in theis_headlessvariable? - Using the variable name
is_headlesssuggests the final value would beTrueorFalse. Is this correct thinking? In what case wouldTrueorFalsebe assigned? - Is
[""]a way to indicateFalse? - A little confused...
CodePudding user response:
- In what way does the blank between
["--headless"]andifcontrol the code line?
You can parse the statement as:
is_headless = <expression>
where <expression> is of the form <true_value> if <condition> else <false_value>. The expression evaluates to either <true_value> or <false_value> depending on the outcome of <condition>, and one of those two values is assigned to is_headless.
- How would
--headlessever be an element in theis_headlessvariable?
The overall effect is to assign one of two values to is_headless, either ["--headless"] or [""]. Curiously, both of these are single-value lists. Let's talk about that...
- Using the variable name
is_headlesssuggests the final value would beTrueorFalse. Is this correct thinking? In what case wouldTrueorFalsebe assigned?
That's exactly what I would expect based on the variable name. Which means it's a bad name. It's not being assigned boolean values so it shouldn't be named is_xxx.
- Is
[""]a way to indicateFalse?
It's supposed to indicate "no argument". As in, don't pass an extra argument to the following command. Having an empty string is probably a mistake; I would prefer to see an empty list [] instead of a list containing an empty string [""].
It would be better if the variable were named simply headless, or headless_argument.
Or, what I would write:
extra_arguments = []
if sys.argv[0].find('console.py') != -1:
extra_arguments.append('--headless')
That's how I would name it: extra_arguments. It's a list of extra arguments that may or may not be passed. By default it's an empty list, and if the condition is true then we add an extra argument --headless to the list.
- A little confused...
I don't blame you!
CodePudding user response:
Well, technically that's a one-liner if/else statement in Python, which is pretty powerful and straightforward enough to use. I think the simplest way of understanding this, is to break the problem down so it's a little bit simpler and more digestible.
For example, substituting sample values such as 'a' and 'b' and using a string instead of arguments passed in the command line, should help make this more clear for illustration purposes:
args = 'python console.py testing'
x = 'a' if args.find('console.py') != -1 else 'b'
print(x)
args = 'python testing'
x = 'a' if args.find('console.py') != -1 else 'b'
print(x)
Out:
a
b
Also, for completeness, to circle back on your other questions:
- When the user's input contains the word
console.py - Yes, the variable naming is not too great. A name of
is_headlessgenerally indicates the var is a boolean type, but here it is not the case, since the var actually is assigned a list of string, which could be annotated as alist[str]for example. - Well, technically
[""]is not falsy, but an empty list[]could be considered falsy. I'm not too clear on why this is set here, perhaps added context might be helpful here however.
