behavior difference for mutable and immutable variable in function definition

J

jianbing.chen

Hi,

Can anyone explain the following:

Python 2.5 (r25:51908, Apr 9 2007, 11:27:23)
[GCC 4.1.1 20060525 (Red Hat 4.1.1-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information..... x = 2
........ x[2] = 2
....Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in bar
NameError: global name 'x' is not defined

Thanks,
Jianbing
 
J

James Stroud

Hi,

Can anyone explain the following:

Python 2.5 (r25:51908, Apr 9 2007, 11:27:23)
[GCC 4.1.1 20060525 (Red Hat 4.1.1-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

... x = 2
...

... x[2] = 2
...

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in bar
NameError: global name 'x' is not defined

Thanks,
Jianbing

1. Each function call creates its own namespace, so "x" in foo() is
"isolated" from the global namespace or from calls of bar().
2. Think of assignment as assigning a name to a value rather than
"putting a value" into the name. When you assign, you completely change
the identity of name, rather than changing the contents of the name.

For example:


py> x = object()
py> id(x)
1074201696
py> x = object()
py> id(x)
1074201704

Notice how the identity (id) of x changes.

James
 
C

Carsten Haese

Hi,

Can anyone explain the following:

Python 2.5 (r25:51908, Apr 9 2007, 11:27:23)
[GCC 4.1.1 20060525 (Red Hat 4.1.1-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.... x = 2
...... x[2] = 2
...Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in bar
NameError: global name 'x' is not defined

"x = 2" binds the name 'x' in foo's local namespace to the object '2'.
For this, it doesn't matter whether the name 'x' was previously bound to
anything.

"x[2] = 2" is a shorthand notation for the method call
"x.__setitem__(2,2)". This requires the name 'x' to be bound to some
object that has a __setitem__ method.

-Carsten
 
7

7stud

Hi,

Can anyone explain the following:

Python 2.5 (r25:51908, Apr 9 2007, 11:27:23)
[GCC 4.1.1 20060525 (Red Hat 4.1.1-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.>>> def foo():

... x = 2
...>>> foo()
... x[2] = 2
...

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in bar
NameError: global name 'x' is not defined

Thanks,
Jianbing

The first function is completely irrelevant unless you expect this to
work:

x = 2
x[2] = 2

Traceback (most recent call last):
File "test1.py", line 2, in ?
x[2] = 2
TypeError: object does not support item assignment

So that leaves you with:
... x[2] = 2
...

Would you expect this to work:

x[2] = 2
print x
 
R

Roger Miller

Can anyone explain the following:
Python 2.5 (r25:51908, Apr 9 2007, 11:27:23)
[GCC 4.1.1 20060525 (Red Hat 4.1.1-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.>>> def foo():
... x = 2
...>>> foo()
... x[2] = 2
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in bar
NameError: global name 'x' is not defined
Thanks,
Jianbing

The first function is completely irrelevant unless you expect this to
work:

x = 2
x[2] = 2

Traceback (most recent call last):
File "test1.py", line 2, in ?
x[2] = 2
TypeError: object does not support item assignment

So that leaves you with:
... x[2] = 2
...

Would you expect this to work:

x[2] = 2
print x

I will sympathize with the OP to the extent that the message "global
name 'x' is not defined" is a bit misleading. All that the interpreter
really knows is that 'x' is not defined, locally or globally, and it
should probably not presume to guess the coder's intention.
 
J

jianbing.chen

Can anyone explain the following:
Python 2.5 (r25:51908, Apr 9 2007, 11:27:23)
[GCC 4.1.1 20060525 (Red Hat 4.1.1-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
def foo():
... x = 2
...
foo()
def bar():
... x[2] = 2
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in bar
NameError: global name 'x' is not defined

"x = 2" binds the name 'x' in foo's local namespace to the object '2'.
For this, it doesn't matter whether the name 'x' was previously bound to
anything.

"x[2] = 2" is a shorthand notation for the method call
"x.__setitem__(2,2)". This requires the name 'x' to be bound to some
object that has a __setitem__ method.

-Carsten

This makes sense.

Thank you.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top