[Solved] More ‘Pythonic’ way to write asymmetric if-else statement


I figure this out finally. Assignment Expressions in Python 3.8 is what I am looking for.

if name in name_dict:
    info = name_dict['name'] ## <-- may be a more complicated function
    if info == '':
        info = input("Input your info: ")
else:
    info = input("Input your info: ")

Can be turn into:

if (name not in name_dict) or (name in name_dict and (info := name_dict['name']) == ''):
    info = input("Input your info: ")

It is like expressing the logic in natural language. ‘If name is not in name dictionary, or the info of name in name dictionary is empty, please input some info.’

solved More ‘Pythonic’ way to write asymmetric if-else statement