Last active
December 8, 2022 22:43
-
-
Save carlsmith/800cbe3e11f630ac8aa0 to your computer and use it in GitHub Desktop.
Main function decorator (for Python 2 and 3).
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
import inspect | |
def main(function): | |
locale = inspect.stack()[1][0].f_locals | |
module = locale.get("__name__", None) | |
if module == "__main__": | |
locale[function.__name__] = function | |
function() | |
return function |
The following modifications work fine for me under 3.10.5
from inspect import stack
def automain( func ) :
locale = stack()[ 1 ].frame.f_locals
module = locale.get( '__name__', None )
if module == '__main__' :
locale[ func.__name__ ] = func
func()
return func
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I haven't used it for years to be honest. I tested it before posting it, but that was ages ago.