Global variables for python applications

TomF tomf.sessile at gmail.com
Thu May 20 14:17:11 EDT 2010



On 2010-05-19 07:34:37 -0700, Steven D'Aprano said:

> # Untested.
> def verbose_print(arg, level, verbosity=1):
>     if level <= verbosity:
>         print arg
> 
> def my_function(arg):
>     my_print(arg, level=2)
>     return arg.upper()
> 
> if __name__ == '__main__':
>     if '--verbose' in sys.argv:
>         my_print = functools.partial(verbose_print, verbosity=2)
>     elif '--quiet' in sys.argv:
>         my_print = functools.partial(verbose_print, verbosity=0)
> 
>     my_function("hello world")
> 
> 
> Note that although there is no verbosity global setting, every function
> that calls my_print will do the right thing (unless I've got the test
> backwards...), and if a function needs to override the implicit verbosity
> setting, it can just call verbose_print directly.





More information about the Python-list mailing list