Global module variables as default parameters

C

Christoph Haas

Hi, list...

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

----------------------------------------
#!/usr/bin/python

globalvar = 123

def test(foo=globalvar):
print foo

test()
----------------------------------------

Running this script prints "123". That's what I expected.

Now I'm trying the same thing in a module context. A non-working example:

test.py
----------------------------------------
#!/usr/bin/python

import TestModule

TestModule.globalvar = 123
TestModule.def1()
TestModule.def2()
----------------------------------------

TestModule.py
----------------------------------------
globalvar = 0

def def1():
print globalvar

def def2(foo=globalvar):
print foo
----------------------------------------

Running the test.py script prints "123" and "0". So accessing the globalvar
in def1() works. But if I try to use the global variable as a default
parameter in def2() it uses the default "0". What is the difference
between these two? Are there contexts of default parameters?

Thanks for any enlightenment.

Christoph
 
M

Marc 'BlackJack' Rintsch

Christoph Haas said:
TestModule.py
----------------------------------------
globalvar = 0

def def1():
print globalvar

def def2(foo=globalvar):
print foo
----------------------------------------

Running the test.py script prints "123" and "0". So accessing the globalvar
in def1() works. But if I try to use the global variable as a default
parameter in def2() it uses the default "0". What is the difference
between these two? Are there contexts of default parameters?

Default parameters are evaluated *once* when the ``def`` is executed. So
in `def2` the value of `foo` won't be looked up when calling the function
as it is already bound to the value 0.

Ciao,
Marc 'BlackJack' Rintsch
 
P

Peter Otten

Christoph said:
Hi, list...

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

----------------------------------------
#!/usr/bin/python

globalvar = 123

def test(foo=globalvar):
print foo

test()
----------------------------------------

Running this script prints "123". That's what I expected.

Now I'm trying the same thing in a module context. A non-working example:

test.py
----------------------------------------
#!/usr/bin/python

import TestModule

TestModule.globalvar = 123
TestModule.def1()
TestModule.def2()
----------------------------------------

TestModule.py
----------------------------------------
globalvar = 0

def def1():
print globalvar

def def2(foo=globalvar):
print foo
----------------------------------------

Running the test.py script prints "123" and "0". So accessing the
globalvar in def1() works. But if I try to use the global variable as a
default parameter in def2() it uses the default "0". What is the
difference between these two? Are there contexts of default parameters?

Yes, and that context is the function definition which is executable code,
too. Whatever the variable 'right' in a statement like

def f(left=right): ...

is bound to when the def is executed will become the default for the 'left'
argument. This should become clear when you create more than one function
with the same
.... def f(i=i): print i
.... fs.append(f)
........
0
1
2

Use a sentinel when you don't want that behaviour:

def f(foo=None):
if foo is None: foo = globalvar
# ...

Peter
 
J

John Hunter

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
 
S

Steve Holden

John said:
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

That's true when the functions are declared at the outer level of the
module, but don't overlook the fact that a function can be declared
inside another function or method call (and even returned as (part of)
the result of that call).

It would be more accurate to say that the default argument values are
bound when the def statement is executed.

regards
Steve
 
C

Christoph Haas

Thanks to all who answered.

Default parameters are evaluated *once* when the ``def`` is executed.
So in `def2` the value of `foo` won't be looked up when calling the
function as it is already bound to the value 0.

Now that you point me to it it's pretty obvious indeed. I always forget
that the 'def's are executed at load time, too.

Peter/John: in fact I already used a sentinel like you proposed as
a "workaround". So there is just one sensible way to do it. Again.

Kindly
Christoph
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top