Close
Community Post

Python Tips'N'Tricks #4

truthness

Python does make a lot of asumptioms about objects, and how to interpret them in specific conditions.

What is important to know is that checking something in an if-statement, will cast the check value to a boolean.
 

if my_value:
    pass
#is the same as
if bool( my_value ):
    pass

On the other hand, a comparision is also a function and actually returns a value you can store.

check_value = 12
my_variable = check_variable == 12
debug( my_variable ) -> Prints True

 

As said in the beginning, python takes assumptions about what to interpret as True and False. For datacollections it behaves the following.

empty_list = [ ] #is False
some_list = [ "Foo" ] #is True
 
empty_dict = { } # is False
some_dict = { "Bana" : "Nana" } #is True
 
empty_set = set() #is False
some_set = { "Touch" } #is True
 
empty_tuple = ( ) #is False
some_tuple = ( "Foo" ) #is True

 

This means, to check is a list is empty, you do not have to check the length, but can just check against the list itself.

if len( my_list ) > 0:
    do_something()
 
if my_list:
    do_something()

Just be aware, that the are also other situations where my_list might not be a list that can be interpreted as True or False too.

  • None
  • 0 Numerics
  • Empty Strings

 

Because None will also be interpreted as false, we can check if an operator-path is valid by simply checking:

my_operator = op( operator_path )
if my_operator: 
    # will be dine when the operator is valid and op() function returns not None
    do_spomething()
else:
    # if the path is invalid, op() returns None that is interpreted as False.

 

This also has some other interresting implications. Because we can, for example, cast nicely between numerics, where False gets cast to 0 and True to 1, and vice versa.

#instead of:
if my_numeric_parameter != 0:
    my_toggle_parameter = True
else
    my_toggle_parameter = False
 
#simply write:
my_toggle_parameter = my_numeric_parameter.eval()

 

This also works for linking parameters of differrent types. For example a videoplayer that only continues the video when the opacity is greater then 0:
 

OR

Another quite interresting function is the OR method. Because of the automatic assumptions python does, OR works a little different. It checks the first value from left to right. If the value is True, it returns the value. If it is false, it returns the next value. The important part is, that it is not casting the value before returning, but checking the truthness.
This gets extremly handy when you work with selectOPs and default values.
 

False or True -> True
False or 1 -> 1
True or 1 -> True
1 or False -> 1
None or My_Operator -> My_Operator
user_input_string or "My Default String" ...

Caviat

Just be aware, that using this techniques can sometimes make it hard to read your code, as it is not always clear what you are checking against explicitly. In general, the idea should be to explicit before implicit. But this can make your code much more readable too, as you do not have to write to many branches.

Comments