473,326 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Global module variables as default parameters

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
Sep 22 '06 #1
5 1500
In <ma**************************************@python.o rg>, Christoph Haas
wrote:
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
Sep 22 '06 #2
Christoph Haas wrote:
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
>>fs = []
for i in range(3):
.... def f(i=i): print i
.... fs.append(f)
....
>>for f in fs: 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
Sep 22 '06 #3
>>>>"Christoph" == Christoph Haas <em***@christoph-haas.dewrites:

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

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

Christophglobalvar = 123

Christophdef 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
Sep 22 '06 #4
John Hunter wrote:
>>>>>>"Christoph" == Christoph Haas <em***@christoph-haas.dewrites:


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

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

Christophglobalvar = 123

Christophdef 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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Sep 23 '06 #5
Thanks to all who answered.

On Friday 22 September 2006 17:28, Marc 'BlackJack' Rintsch wrote:
Christoph Haas wrote:
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.
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
Sep 23 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

59
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
8
by: newbie | last post by:
Hello, I have questions about global variables in OOP (in general) and Python (in specific). I understand (I think) that global variables are generally not a good idea. However, if there are...
0
by: Gary Herron | last post by:
Jacob Davis wrote: Yuck, YUCK, YUCK! You are breaking *so* many good-programming-practices, I hardly know where to start. First off: A python global is not what you think. There are *no*...
6
by: pistacchio | last post by:
hi to all! can i load a module passing to it, automatically and as default, all the caller's global variables to act as module's global variables? thanks
4
by: icarus | last post by:
global_vars.py has the global variables set_var.py changes one of the values on the global variables (don't close it or terminate) get_var.py retrieves the recently value changed (triggered right...
14
by: Fred Block | last post by:
Hi All, I'm an experienced VB6 developer and now starting (newbee) with VB 2008 and I'm very excited. Here's an issue I'm experiencing right off the starting line and cannot make sense of it:...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.