Namespace issues...

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

    Namespace issues...

    i am very confused...why does the following script modify the global list "l":

    l=[]

    def x():
    l.append("xyz")

    x()
    print l

    but the same script applied to a single variable doesnt..:

    l="moe"

    def x():
    l+="howdy"

    x()
    print l
  • vincent wehren

    #2
    Re: Namespace issues...


    "cghost" <phreacker@aol. com> schrieb im Newsbeitrag
    news:d1253dfc.0 402140259.3b614 b95@posting.goo gle.com...
    | i am very confused...why does the following script modify the global list
    "l":
    |
    | l=[]
    |
    | def x():
    | l.append("xyz")
    |
    | x()
    | print l
    |
    | but the same script applied to a single variable doesnt..:
    |
    | l="moe"
    |
    | def x():
    | l+="howdy"

    Python decides that a variable is local if it's ever *assigned a value in a
    function*.
    In this example you assign a value to l. In the first example, you do not
    (you change list l
    "in place"), so here this optimization does not take place.

    You have to be explicit about l being a global if you assign a value to it
    using the "global" statement as in:
    def x():
    global l
    l+="howdy"

    HTH,

    Vincent Wehren



    |
    | x()
    | print l


    Comment

    • Peter Otten

      #3
      Re: Namespace issues...

      cghost wrote:
      [color=blue]
      > i am very confused...why does the following script modify the global list
      > "l":
      >
      > l=[]
      >
      > def x():
      > l.append("xyz")
      >
      > x()
      > print l
      >
      > but the same script applied to a single variable doesnt..:
      >
      > l="moe"
      >
      > def x():
      > l+="howdy"
      >
      > x()
      > print l[/color]

      It's even worse:
      [color=blue][color=green][color=darkred]
      >>> def add(a, b):[/color][/color][/color]
      .... a += b
      ....

      Now consider
      [color=blue][color=green][color=darkred]
      >>> lst = []
      >>> add(lst, ["alpha"])
      >>> lst[/color][/color][/color]
      ['alpha'][color=blue][color=green][color=darkred]
      >>> add(lst, ["beta", "gamma"])
      >>> lst[/color][/color][/color]
      ['alpha', 'beta', 'gamma']

      where add(a, b) appends b to a versus
      [color=blue][color=green][color=darkred]
      >>> tpl = ()
      >>> add( tpl, ("alpha",))
      >>> tpl[/color][/color][/color]
      ()

      where no apparent change takes place. The difference is that list is a
      mutable type i. e. its instances can be changed anytime, whereas tuples are
      immutable, i. e. they cannot be changed once they are created. For lists a
      += b is implemented to append the items in b to a. The binding of a is not
      changed in the process. For immutable types this is not an option - they
      cannot be altered. Therefore, for tuples a += b is implemented as a = a +
      b, i. e. a new tuple containing the elements of both a and b is created and
      the variable a is bound to the new tuple. In the example the binding takes
      place inside a function, so you never see the new tuple unless you return
      it or declare the variable as global as Vincent already pointed out.
      Strings are immutable too, so
      [color=blue][color=green][color=darkred]
      >>> s = "abc"
      >>> add(s, "def")
      >>> s[/color][/color][/color]
      'abc'

      as expected (hopefully).

      Peter

      Comment

      • Aahz

        #4
        Re: Namespace issues...

        In article <d1253dfc.04021 40259.3b614b95@ posting.google. com>,
        cghost <phreacker@aol. com> wrote:[color=blue]
        >
        >i am very confused...why does the following script modify the global list "l":
        >
        >l=[]
        >def x():
        > l.append("xyz")
        >x()
        >print l
        >
        >but the same script applied to a single variable doesnt..:
        >
        >l="moe"
        >def x():
        > l+="howdy"
        >x()
        >print l[/color]


        --
        Aahz (aahz@pythoncra ft.com) <*> http://www.pythoncraft.com/

        "Argue for your limitations, and sure enough they're yours." --Richard Bach

        Comment

        Working...