Home > Software engineering >  Setting a variable with various conditional statements
Setting a variable with various conditional statements

Time:12-10

I have this python coded statement:

is_headless = ["--headless"] if sys.argv[0].find('console.py') != -1 else [""]
  1. In what way does the blank between ["--headless"] and if control the code line?
  2. How and would "--headless" ever be an element in the is_headless variable?
  3. Using the variable name is_headless suggests the final value would be True or False. Is this correct thinking? In what case would True or False be assigned?
  4. Is [""] a way to indicate False?
  5. A little confused...

CodePudding user response:

  1. In what way does the blank between ["--headless"] and if control 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.

  1. How would --headless ever be an element in the is_headless variable?

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...

  1. Using the variable name is_headless suggests the final value would be True or False. Is this correct thinking? In what case would True or False be 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.

  1. Is [""] a way to indicate False?

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.

  1. 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:

  1. When the user's input contains the word console.py
  2. Yes, the variable naming is not too great. A name of is_headless generally 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 a list[str] for example.
  3. 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.
  • Related