Last active
October 25, 2022 15:32
-
-
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.`
This file contains 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 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