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

S

seberino

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
 
R

Robert Kern

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
(e-mail address removed)

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

Paolino

Robert said:
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
 
J

John Roth

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
 

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,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top