Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #26849

Re: [newbie] A question about lists and strings

Subject Re: [newbie] A question about lists and strings
From Roman Vashkevich <vashkevichrb@gmail.com>
Date 2012-08-10 13:28 +0400
References <k02jn0$id2$1@news.albasani.net>
Newsgroups comp.lang.python
Message-ID <mailman.3146.1344590943.4697.python-list@python.org> (permalink)

Show all headers | View raw


10.08.2012, в 13:19, Mok-Kong Shen написал(а):

> 
> In an earlier question about lists, I was told about the issue of
> creation of local names in a function. However, I still can't
> understand why the program below outputs:
> 
> [999] sss
> [999]
> 
> and not two identical lines of output. For both operators "+=" should
> anyway work in similar manner in the function xx in my view.
> 
> Thanks for your help in advance.
> 
> M. K. Shen
> 
> ----------------------------------------------------------
> 
> def xx(list,str):
>  list+=[999]
>  str+="sss"
> 
> lista=[]
> stra=""
> lista+=[999]
> stra+="sss"
> print(lista,stra)
> 
> listb=[]
> strb=""
> xx(listb,strb)
> print(listb,strb)
> -- 
> http://mail.python.org/mailman/listinfo/python-list

It seems like your xx() function doesn't return local str parameter and prints the global empty str, whereas it mutates the global list.

Roman

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[newbie] A question about lists and strings Mok-Kong Shen <mok-kong.shen@t-online.de> - 2012-08-10 11:19 +0200
  Re: [newbie] A question about lists and strings Roman Vashkevich <vashkevichrb@gmail.com> - 2012-08-10 13:28 +0400
  Re: [newbie] A question about lists and strings Roman Vashkevich <vashkevichrb@gmail.com> - 2012-08-10 13:48 +0400
    Re: [newbie] A question about lists and strings Mok-Kong Shen <mok-kong.shen@t-online.de> - 2012-08-10 12:12 +0200
      Re: [newbie] A question about lists and strings Chris Angelico <rosuav@gmail.com> - 2012-08-10 20:37 +1000
      Re: [newbie] A question about lists and strings Dave Angel <d@davea.name> - 2012-08-10 06:37 -0400
      Re: [newbie] A question about lists and strings Roman Vashkevich <vashkevichrb@gmail.com> - 2012-08-10 14:56 +0400
        Re: [newbie] A question about lists and strings Mok-Kong Shen <mok-kong.shen@t-online.de> - 2012-08-10 13:08 +0200
      Re: [newbie] A question about lists and strings Dave Angel <d@davea.name> - 2012-08-10 06:56 -0400
      Re: [newbie] A question about lists and strings Chris Angelico <rosuav@gmail.com> - 2012-08-10 21:00 +1000
      Re: [newbie] A question about lists and strings Roman Vashkevich <vashkevichrb@gmail.com> - 2012-08-10 15:06 +0400
  Re: [newbie] A question about lists and strings Peter Otten <__peter__@web.de> - 2012-08-10 11:59 +0200
    Re: [newbie] A question about lists and strings Rotwang <sg552@hotmail.co.uk> - 2012-08-10 16:12 +0100
  Re: [newbie] A question about lists and strings Dave Angel <d@davea.name> - 2012-08-10 06:07 -0400
    Re: [newbie] A question about lists and strings Mok-Kong Shen <mok-kong.shen@t-online.de> - 2012-08-10 12:31 +0200
      Re: [newbie] A question about lists and strings Chris Angelico <rosuav@gmail.com> - 2012-08-10 20:40 +1000
        Re: [newbie] A question about lists and strings Mok-Kong Shen <mok-kong.shen@t-online.de> - 2012-08-10 12:48 +0200
          Re: [newbie] A question about lists and strings Dave Angel <d@davea.name> - 2012-08-10 06:58 -0400
      Re: [newbie] A question about lists and strings Dave Angel <d@davea.name> - 2012-08-10 06:53 -0400

csiph-web