Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   behavior difference for mutable and immutable variable in function definition (http://www.velocityreviews.com/forums/t504224-behavior-difference-for-mutable-and-immutable-variable-in-function-definition.html)

jianbing.chen@gmail.com 05-04-2007 09:30 PM

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


James Stroud 05-04-2007 10:07 PM

Re: behavior difference for mutable and immutable variable in functiondefinition
 
jianbing.chen@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:


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

Notice how the identity (id) of x changes.

James

Carsten Haese 05-04-2007 10:14 PM

Re: behavior difference for mutable and immutable variable infunction definition
 
On Fri, 2007-05-04 at 14:30 -0700, jianbing.chen@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


7stud 05-04-2007 10:39 PM

Re: behavior difference for mutable and immutable variable in function definition
 
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




Roger Miller 05-05-2007 01:03 AM

Re: behavior difference for mutable and immutable variable in function definition
 
On May 4, 12:39 pm, 7stud <bbxx789_0...@yahoo.com> wrote:
> 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.



jianbing.chen@gmail.com 05-05-2007 08:05 PM

Re: behavior difference for mutable and immutable variable in function definition
 
On May 4, 5:14 pm, Carsten Haese <cars...@uniqsys.com> wrote:
> 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.



All times are GMT. The time now is 11:10 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57