Last active
August 24, 2020 17:59
-
-
Save mgeraci/60ac6b1b16d309fe29aa415b372d2401 to your computer and use it in GitHub Desktop.
Examples for a few strategies for returning values from 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 redefine(user_input): | |
| result = None | |
| if user_input < 2: | |
| result = user_input | |
| elif user_input < 5: | |
| result = user_input * 2 | |
| else: | |
| result = user_input * 3 | |
| return result | |
| def short_circuit(user_input): | |
| if user_input < 2: | |
| return user_input | |
| elif user_input < 5: | |
| return user_input * 2 | |
| else: | |
| return user_input * 3 | |
| def short_circuit_even_better(user_input): | |
| if user_input < 2: | |
| return user_input | |
| if user_input < 5: | |
| return user_input * 2 | |
| return user_input * 3 | |
| print("redefine") | |
| print(redefine(1)) | |
| print(redefine(3)) | |
| print(redefine(8)) | |
| print("") | |
| print("short_circuit") | |
| print(short_circuit(1)) | |
| print(short_circuit(3)) | |
| print(short_circuit(8)) | |
| print("") | |
| print("short_circuit_even_better") | |
| print(short_circuit_even_better(1)) | |
| print(short_circuit_even_better(3)) | |
| print(short_circuit_even_better(8)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment