I have a variable I want to set depending on the values in three booleans. The most straight-forward way is an if statement followed by a series of elifs:
if a and b and c: name = 'first' elif a and b and not c: name = 'second' elif a and not b and c: name = 'third' elif a and not b and not c: name = 'fourth' elif not a and b and c: name = 'fifth' elif not a and b and not c: name = 'sixth' elif not a and not b and c: name = 'seventh' elif not a and not b and not c: name = 'eighth'
This is a bit awkward, and I'm wondering if there's a more Pythonic way to handle this problem. A couple of ideas come to mind.
Dictionary hack:
name = {a and b and c: 'first', a and b and not c: 'second', a and not b and c: 'third', a and not b and not c: 'fourth', not a and b and c: 'fifth', not a and b and not c: 'sixth', not a and not b and c: 'seventh', not a and not b and not c: 'eighth'}[True]
I call it a hack because I'm not too wild about seven of the keys being False and overriding each other.
And/or magic
name = (a and b and c and 'first' or a and b and not c and 'second' or a and not b and c and 'third' or a and not b and not c and 'fourth' or not a and b and c and 'fifth' or not a and b and not c and 'sixth' or not a and not b and c and 'seventh' or not a and not b and not c and 'eighth')
This works because Python ands and ors return the last value to be evaluated, but you have to know that in order to understand this otherwise bizarre code.
None of these three options is very satisfying. What do you recommend?
The Python Boolean type is one of Python's built-in data types. It's used to represent the truth value of an expression. For example, the expression 1 <= 2 is True , while the expression 0 == 1 is False .
If you want to define a boolean in Python, you can simply assign a True or False value or even an expression that ultimately evaluates to one of these values. You can check the type of the variable by using the built-in type function in Python.
We can evaluate values and variables using the Python bool() function. This method is used to return or convert a value to a Boolean value i.e., True or False, using the standard truth testing procedure.
You can check if a value is either truthy or falsy with the built-in bool() function. According to the Python Documentation, this function: Returns a Boolean value, i.e. one of True or False . x (the argument) is converted using the standard truth testing procedure.
You can think of a, b, and c as three bits that when put together form a number between 0 and 7. Then, you can have an array of the values ['first', 'second', ... 'eighth'] and use the bit value as an offset into the array. This would just be two lines of code (one to assemble the bits into a value from 0-7, and one to lookup the value in the array).
Here's the code:
nth = ['eighth', 'seventh', 'sixth', 'fifth', 'fourth', 'third', 'second', 'first'] nth[(a and 4 or 0) | (b and 2 or 0) | (c and 1 or 0)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With