behavior difference for mutable and immutable variable in function definition

Discussion in 'Python' started by jianbing.chen@gmail.com, May 4, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. James Stroud Guest

    Re: behavior difference for mutable and immutable variable in functiondefinition

    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
    James Stroud, May 4, 2007
    #2
    1. Advertising

  3. Re: behavior difference for mutable and immutable variable infunction definition

    On Fri, 2007-05-04 at 14:30 -0700, 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
    Carsten Haese, May 4, 2007
    #3
  4. 7stud Guest

    On May 4, 3:30 pm, 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
    7stud, May 4, 2007
    #4
  5. Roger Miller Guest

    On May 4, 12:39 pm, 7stud <> wrote:
    > On May 4, 3:30 pm, 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.
    Roger Miller, May 5, 2007
    #5
  6. Guest

    On May 4, 5:14 pm, Carsten Haese <> wrote:
    > On Fri, 2007-05-04 at 14:30 -0700, 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, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    4
    Views:
    482
    Oliver Wong
    Mar 8, 2006
  2. Replies:
    2
    Views:
    368
    John Roth
    Apr 15, 2004
  3. Pierre Yves
    Replies:
    2
    Views:
    474
    Pierre Yves
    Jan 10, 2008
  4. Bernard Lim

    Immutable and Mutable Types

    Bernard Lim, Mar 17, 2008, in forum: Python
    Replies:
    16
    Views:
    635
    Terry Reedy
    Mar 19, 2008
  5. Deb Wyatt

    immutable vs mutable

    Deb Wyatt, Jun 3, 2014, in forum: Python
    Replies:
    13
    Views:
    53
    Mark H Harris
    Jun 4, 2014
Loading...

Share This Page