Skip to content

Instantly share code, notes, and snippets.

@GeroZayas
Last active August 19, 2025 06:35
Show Gist options
  • Select an option

  • Save GeroZayas/1c9007f38e8f0aff2dd42179eb239438 to your computer and use it in GitHub Desktop.

Select an option

Save GeroZayas/1c9007f38e8f0aff2dd42179eb239438 to your computer and use it in GitHub Desktop.
# Stupid simple idea but very useful
# if some condition is met, then we return new value, or charged old value
# Else, we return the (unchanged) original value
def change_if_new(new_thing, old_thing):
if new_thing:
return new_thing
# else
return old_thing
name = "Gero"
print(name) # Gero
name = change_if_new(new_thing="Enrique", old_thing=name)
print(name) # Enrique
name = change_if_new(new_thing=None, old_thing=name) # No change
name = change_if_new(new_thing=0, old_thing=name) # No change
name = change_if_new(new_thing=False, old_thing=name) # No change
print(name) # Enrique
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment