Last active
July 8, 2022 16:28
-
-
Save onelharrison/0125fa66dac814eb344d06c6ae34a5ac to your computer and use it in GitHub Desktop.
Code snippet showing pure vs impure functions in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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