Variable Scope

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jens Thiede

    Variable Scope

    In the following terminal; could someone inform me as to why it is
    posible to print a global variable without having to declare it using
    global. This has affected some source of mine, and allows you to
    modify a global in a local scope *without* the global keyword, for
    instance, you can append to a global list, but *not* assign it a new
    value, for then, you create a new local variable. -- Why.

    Python 2.3.2 (#1, Jan 3 2004, 23:02:08)
    [GCC 3.2.3 20030422 (Gentoo Linux 1.4 3.2.3-r3, propolice)] on linux2

    IDLE 1.0[color=blue][color=green][color=darkred]
    >>> x = 10;
    >>> def test():[/color][/color][/color]
    x += 1;

    [color=blue][color=green][color=darkred]
    >>> test();[/color][/color][/color]

    Traceback (most recent call last):
    File "<pyshell#5 >", line 1, in -toplevel-
    test();
    File "<pyshell#4 >", line 2, in test
    x += 1;
    UnboundLocalErr or: local variable 'x' referenced before assignment[color=blue][color=green][color=darkred]
    >>> def test2():[/color][/color][/color]
    print x;

    [color=blue][color=green][color=darkred]
    >>> test2();[/color][/color][/color]
    10

    Any help would be appreciated,

    Jens Thiede.
  • anton muhin

    #2
    Re: Variable Scope

    Jens Thiede wrote:
    [color=blue]
    > In the following terminal; could someone inform me as to why it is
    > posible to print a global variable without having to declare it using
    > global. This has affected some source of mine, and allows you to
    > modify a global in a local scope *without* the global keyword, for
    > instance, you can append to a global list, but *not* assign it a new
    > value, for then, you create a new local variable. -- Why.
    >
    > Python 2.3.2 (#1, Jan 3 2004, 23:02:08)
    > [GCC 3.2.3 20030422 (Gentoo Linux 1.4 3.2.3-r3, propolice)] on linux2
    >
    > IDLE 1.0
    >[color=green][color=darkred]
    >>>>x = 10;
    >>>>def test():[/color][/color]
    >
    > x += 1;
    >
    >
    >[color=green][color=darkred]
    >>>>test();[/color][/color]
    >
    >
    > Traceback (most recent call last):
    > File "<pyshell#5 >", line 1, in -toplevel-
    > test();
    > File "<pyshell#4 >", line 2, in test
    > x += 1;
    > UnboundLocalErr or: local variable 'x' referenced before assignment
    >[color=green][color=darkred]
    >>>>def test2():[/color][/color]
    >
    > print x;
    >
    >
    >[color=green][color=darkred]
    >>>>test2();[/color][/color]
    >
    > 10
    >
    > Any help would be appreciated,
    >
    > Jens Thiede.[/color]

    I suppose because Python prohibits rebinding of global variables, not
    modifications. You actually cannot change an integera (as an immutable
    type) with rebinding a varibale. Lists (as mutable types) allows
    modifications without rebinding. E.g.

    l = []

    def f1():
    l.append(0)

    def f2():
    l = l + [0]

    f1()
    print l

    f2()
    print l

    Python says:
    [0]
    Traceback (most recent call last):
    File "1.py", line 12, in ?
    f2()
    File "1.py", line 7, in f2
    l = l + [0]
    UnboundLocalErr or: local variable 'l' referenced before assignment

    regards,
    anton.

    Comment

    • Peter Hansen

      #3
      Re: Variable Scope

      Jens Thiede wrote:[color=blue]
      >
      > In the following terminal; could someone inform me as to why it is
      > posible to print a global variable without having to declare it using
      > global. This has affected some source of mine, and allows you to
      > modify a global in a local scope *without* the global keyword, for
      > instance, you can append to a global list, but *not* assign it a new
      > value, for then, you create a new local variable. -- Why.[/color]

      Have you read http://www.python.org/doc/faq/progra...-in-a-function ?

      It should clarify some of these issues. If it does not, please let us
      know what's still unclear and the FAQ can be improved.

      -Peter

      Comment

      Working...