Skip to content

Instantly share code, notes, and snippets.

@thegame4craft
Last active October 25, 2022 15:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thegame4craft/43828d3f42c6fdf35c7a2e75cad65486 to your computer and use it in GitHub Desktop.
Save thegame4craft/43828d3f42c6fdf35c7a2e75cad65486 to your computer and use it in GitHub Desktop.
This Gist contains one function and a decorator. As you see in the example, you do not need an `if __name__ == "__main__:" to call any statement.`
def mainFunc(func): # func is the function
result = func() # which should be called at begin.
# Note: Python executes the file from top to bottom. so every statment that is not in a function (like
# `print`) will be executed before the main function if it is earlyer defined
def wrapper():
nonlocal result
print(result) # print the result
return result # and return it
return wrapper() # call wrapper function
# Note: while the function is called in the `mainFunc` scope, you shouldn't call it here
# except you want an infinite loop
# Note 2: if you do not need the wrapper (and the function result) you can just remove the parentheses
@mainFunc
def main():
print("Hello World")
return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment