[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Boolean comparison & PEP8

On 7/29/19 10:02 AM, David Raymond wrote:

 > I think the other part of the discussion to be had here is: how do you
 > name your booleans?


 > ... To me the name of a boolean variable should be obvious that it's a
 > boolean ...

Well, yeah, maybe.  If it's really only a boolean, and its value is
always either True or False, then I agree.

 > if shell:

 > #wait, "shell" is not really a statement or an instruction. "If
 > shell"... what? "I need to shell"? Is this whether we want to use a
 > shell, or if we discovered that we're already in one? Or is "shell"
 > not really a boolean, and is holding which shell we want, and we're
 > just using the "truthyness" to make sure it's not None? What's going
 > on here? Dangit, ok, where's the documentation?  (subprocess is common
 > enough that people know this, but imagine a much less frequently used
 > module/function)

 > Do I have a point or am I just smoking crack? Does anyone else have
 > examples?

Consider an algorithm like this to determine which shell to use:

     shell = (get_shell_from_command_line()
              or get_shell_from_project_preferences()
              or get_shell_from_personal_preferences()
              or get_shell_from_system_preferences())

where each get_shell_from_XXX() function returns a /path/to/shell string
or None.  At least to me, Python's "or" operator and its notion of
truthiness make this algorithm natural.  Perhaps a better name is
path_to_shell, but I'd still end up with:

     if path_to_shell:
         execute_program(path_to_shell, ...)
         raise NoShellException()