Last active
January 17, 2024 09:13
-
-
Save agalazis/f060768aaa15f9cedee6ba7497b32f30 to your computer and use it in GitHub Desktop.
Python default arguments example
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
# python defaults | |
import sys | |
def value_generator(): | |
yield "first_value" | |
yield "this will never be returned because I am called once on parse" | |
values=value_generator() | |
def foo(): | |
print("executing foo look at me I am called before runnig main :p") | |
return next(values) | |
def fn_with_default(x=foo()): | |
print(f"executing fn_with_default which returned {x} from before running main because it was executed when script was parsed :P") | |
def main(): | |
print("running main") | |
fn_with_default() | |
fn_with_default() | |
if __name__ == '__main__': | |
main() | |
# https://python-fiddle.com/saved/iOgOfgILeQWUKJJcF7po |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment