473,396 Members | 2,111 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,396 software developers and data experts.

behavior difference for mutable and immutable variable in function definition

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()
def bar():
.... x[2] = 2
....
>>>
bar()
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

May 4 '07 #1
5 1914
ji***********@gmail.com wrote:
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()
def bar():

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

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:
pyx = object()
pyid(x)
1074201696
pyx = object()
pyid(x)
1074201704

Notice how the identity (id) of x changes.

James
May 4 '07 #2
On Fri, 2007-05-04 at 14:30 -0700, ji***********@gmail.com wrote:
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()
def bar():
... x[2] = 2
...
>>
bar()
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

May 4 '07 #3
On May 4, 3:30 pm, jianbing.c...@gmail.com wrote:
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()
>def bar():

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

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:
>def bar():

... x[2] = 2
...
>bar()
Would you expect this to work:

x[2] = 2
print x

May 4 '07 #4
On May 4, 12:39 pm, 7stud <bbxx789_0...@yahoo.comwrote:
On May 4, 3:30 pm, jianbing.c...@gmail.com wrote:
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()
>>def bar():
... x[2] = 2
...
>>bar()
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:
>>def bar():
... x[2] = 2
...
>>bar()

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.
May 5 '07 #5
On May 4, 5:14 pm, Carsten Haese <cars...@uniqsys.comwrote:
On Fri, 2007-05-04 at 14:30 -0700, jianbing.c...@gmail.com wrote:
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()
>>def bar():
... x[2] = 2
...
>>bar()
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.

May 5 '07 #6

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

Similar topics

17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
12
by: Kjetil Kristoffer Solberg | last post by:
What is a mutable struct? regards Kjetil Kristoffer Solberg
12
by: Water Cooler v2 | last post by:
Are JavaScript strings mutable? How're they implemented - 1. char arrays 2. linked lists of char arrays 3. another data structure I see that the + operator is overloaded for the string class...
12
by: Vincent RICHOMME | last post by:
Hi, I am currently implementing some basic classes from .NET into modern C++. And I would like to know if someone would know a non mutable string class.
4
by: Muthu Arumugam | last post by:
Tried the following c# code static void Main(string args) { ArrayList list = new ArrayList(); int i = 10;
6
by: tkpmep | last post by:
I have written a program that runs portfolio simulations with different parameters and prints the output, but am mystified by the behavior of a mutable class variable. A simplified version of the...
7
by: Shafik | last post by:
Hello folks, I am an experienced programmer, but very new to python (2 days). I wanted to ask: what exactly is the difference between a tuple and a list? I'm sure there are some, but I can't...
5
by: narutocanada | last post by:
hi what is the difference between the two kinds of brackets? I tried a few examples but I can't make out any real difference: lst = print lst print lst print lst lst = (10, 20, 30) print...
35
by: bukzor | last post by:
I've found some bizzare behavior when using mutable values (lists, dicts, etc) as the default argument of a function. I want to get the community's feedback on this. It's easiest to explain with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.