473,496 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

why functions in modules need 'global foo' for integer foo but not dictionary foo?

At top of a module I have an integer like so...

foo = 4

In a function in that module I know I need to do 'global foo' to get at
the value 4.
....

IIRC, for dictionaries you DO NOT have this issue?

Why this scope problem with integers but not dictionaries?

Chris

Jul 29 '05 #1
3 1472
se******@spawar.navy.mil wrote:
At top of a module I have an integer like so...

foo = 4

In a function in that module I know I need to do 'global foo' to get at
the value 4.
...
You don't. You need the global declaration to *set* the value of "foo"
in the module's namespace. When getting the value of "foo" within the
function, if it's not been defined in the function's local namespace, it
automatically looks in the global namespace with or without the "global
foo" declaration. When assigning a value to the name "foo" within the
function, however, without the global declaration, the value will only
be assigned to the name "foo" within the local namespace.

http://docs.python.org/ref/naming.html
IIRC, for dictionaries you DO NOT have this issue?

Why this scope problem with integers but not dictionaries?


I presume you are trying code like the following:

foo = 4
bar = {}

def fun1():
foo = 5

def fun2():
bar['baz'] = 4

Since integers are immutable, all that fun1() does is assign 5 to the
name "foo" within fun1()'s namespace and doesn't touch the module-level
namespace.

fun2(), on the other hand, only gets the dictionary from the
module-level namespace with the name "bar". Then it modifies that
dictionary (since it's mutable). It does not try to assign a new object
to the name "bar".

I hope that's clear, but I'm pretty tired right now, and it may not be.

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Jul 29 '05 #2
Robert Kern wrote:
se******@spawar.navy.mil wrote:
At top of a module I have an integer like so...

foo = 4

In a function in that module I know I need to do 'global foo' to get at
the value 4.
...


I presume you are trying code like the following:

foo = 4
bar = {}

def fun1():
foo = 5

def fun2():
bar['baz'] = 4

Since integers are immutable, all that fun1() does is assign 5 to the
name "foo" within fun1()'s namespace and doesn't touch the module-level
namespace. Hmm this is obscure to me also, what mutables has to do with this
problem?A binding is always mutable.
fun2(), on the other hand, only gets the dictionary from the
module-level namespace with the name "bar". Then it modifies that
dictionary (since it's mutable). It does not try to assign a new object
to the name "bar".


Probably the point is modifing/rebinding the bound values and not
rebind them.

Generally if you need to use globals for inter-instance/class
communication, I suggest to define a new namespace where
to put global bindings:

class Globals:
foo=4
spam={}

for lexical coherence you can put also funtions there

class Globals:
foo=4
spam={}

@staticmethod
def func():pass

then you always refer to them with Globals.foo,Globals.spam,Globals.func
or with <Module>.Globals.... from outside.

Finally consider to use singletons as a more common approach.

Ciao

___________________________________
Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB
http://mail.yahoo.it
Jul 29 '05 #3
<se******@spawar.navy.mil> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
At top of a module I have an integer like so...

foo = 4

In a function in that module I know I need to do 'global foo' to get at
the value 4.
Actually, you don't need a "global foo" statement to
_access_ the value. You do need a "global foo" to
_rebind_ the value.
...

IIRC, for dictionaries you DO NOT have this issue?

Why this scope problem with integers but not dictionaries?
Telling an object to mutate itself doesn't rebind the object.
so, if you wanted to say:

foo = 5

and have it work at the module level, you'd need a global foo
statement, while

foo["bar"] = "spam"

doesn't. The dictionary "foo" isn't rebound, all that's happening
is that its state is being changed (that is, the key and value is
being added to the dictionary).

HTH

John Roth

Chris


Jul 29 '05 #4

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

Similar topics

0
1300
by: OKB (not okblacke) | last post by:
I'm fooling around with some MUD server code, and I want to add a "reload" command that will let MUD wizards reload the server modules, so that changes to the MUD parser and such can be effected...
99
5814
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
37
2092
by: Kay Schluehr | last post by:
Since George Sakkis proposed a new way of doing list comprehensions http://groups-beta.google.com/group/comp.lang.python/browse_frm/thread/ac5023ad18b2835f/d3ff1b81fa70c8a7#d3ff1b81fa70c8a7 ...
6
1801
by: Mayer | last post by:
Hello: Is there a way to see at the python prompt the names of all the public methods of a class or the names exported by a module? I know that GUI-based IDEs have a nifty way of displaying...
27
2626
by: Maximus | last post by:
Hi, I was just wondering, is it good to use return without arguments in a void function as following: void SetMapLayer() { if( !Map ) return; layer = LAYER_MAP; }
13
2320
by: Robin Haswell | last post by:
Hey people I'm an experience PHP programmer who's been writing python for a couple of weeks now. I'm writing quite a large application which I've decided to break down in to lots of modules...
5
1382
by: jonkersbart | last post by:
Dear, I have wrote a script and want to group some functions of the script in a separate modulo so that I can import the module in other scripts and use the same functions there.. The problem...
14
5974
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
3
1866
by: taps128 | last post by:
I've been reading the namespace specification for the 5.3 relaese, and I can't stop thinking that they have complicated the thing unecessary. Here is what I mean. So far if you call a function...
0
6991
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7196
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6878
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4897
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4583
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
649
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.