By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,495 Members | 1,307 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,495 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
<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 discussion thread is closed

Replies have been disabled for this discussion.