Apologies in advance for the obscure title. I wasn't sure how to phrase what I encountered.
Imagine that you have a title of a book alongside its author, separated by -, in a variable title_author. You scraped this information from the web so it might very well be that this item is None. Obviously you would like to separate the title from the author, so you'd use split. But in case title_author is None to begin with, you just want both title and author to be None.
I figured that the following was a good approach:
title_author = "In Search of Lost Time - Marcel Proust"
title, author = title_author.split("-", 1) if title_author else None, None
print(title, author)
# ['In Search of Lost Time ', ' Marcel Proust'] None
But to my surprise, title now was the result of the split and author was None. The solution is to explicitly indicate that the else clause is a tuple by means of parentheses.
title, author = title_author.split("-", 1) if title_author else (None, None)
print(title, author)
# In Search of Lost Time Marcel Proust
So why is this happening? What is the order of execution here that lead to the result in the first case?