Skip to content

Instantly share code, notes, and snippets.

@onelharrison
Last active July 8, 2022 16:28
Show Gist options
  • Select an option

  • Save onelharrison/0125fa66dac814eb344d06c6ae34a5ac to your computer and use it in GitHub Desktop.

Select an option

Save onelharrison/0125fa66dac814eb344d06c6ae34a5ac to your computer and use it in GitHub Desktop.
Code snippet showing pure vs impure functions in Python
def strip_pure(sentence: str) -> str:
return sentence.strip()
strip_impure_call_count = 0
def strip_impure(sentence: str) -> str:
global strip_impure_call_count
stripped_sentence = sentence.strip()
# Side effect 1: modify global state
strip_impure_call_count += 1
# Side effect 2: output to stdout
print(f"Called strip_impure {strip_call_count} times")
return stripped_sentence
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment