Skip to content

Instantly share code, notes, and snippets.

@agalazis
Last active January 17, 2024 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agalazis/f060768aaa15f9cedee6ba7497b32f30 to your computer and use it in GitHub Desktop.
Save agalazis/f060768aaa15f9cedee6ba7497b32f30 to your computer and use it in GitHub Desktop.
Python default arguments example
# 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