Good Code
The good version treats None as the missing case and preserves an intentionally empty nickname.
Lesson 02
Distinguish missing values from valid falsy values such as empty strings, zero, and empty collections.
def display_name(user):
# None means the caller did not provide a nickname.
if user.nickname is None:
return user.full_name
return user.nicknamedef display_name(user):
# This treats "", 0, False, and None as the same case.
if user.nickname:
return user.nickname
return user.full_nameThe good version treats None as the missing case and preserves an intentionally empty nickname.
The bad version treats every falsy value as missing. Empty strings, zero, empty lists, and False can accidentally take the fallback path.
is None when absence is different from an empty value.