Global module variables as default parameters

John Hunter jdhunter at ace.bsd.uchicago.edu
Fri Sep 22 16:01:48 EDT 2006


>>>>> "Christoph" == Christoph Haas <email at christoph-haas.de> writes:

    Christoph> Hi, list...  I wondered if it's possible to use global
    Christoph> (module) variables as default parameters. A simple
    Christoph> working example:

    Christoph> ----------------------------------------
    Christoph> #!/usr/bin/python

    Christoph> globalvar = 123

    Christoph> def test(foo=globalvar): print foo

kwargs defaults are initialized a module load time, not at function
call time.  The standard idiom is

def test(foo=None): 
    if foo is None: foo = globalvar
    print foo

JDH



More information about the Python-list mailing list