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


Groups > comp.lang.python > #83693

Re: Help understanding list operatoins inside functions in python 3

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <mortoxa@gmx.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.012
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; '[1,': 0.09; 'function,': 0.09; 'subject:Help': 0.11; 'def': 0.12; 'exist.': 0.16; 'received:gmx.com': 0.16; 'subject: \n ': 0.16; 'subject:skip:u 10': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'creating': 0.23; 'header:User-Agent:1': 0.23; 'exists': 0.24; 'this:': 0.26; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'specifically': 0.29; '[1]': 0.29; 'subject:list': 0.30; "i'm": 0.30; 'from:addr:gmx.com': 0.31; 'could': 0.34; 'data,': 0.36; 'list': 0.37; 'to:addr:python-list': 0.38; 'anything': 0.39; 'does': 0.39; '\xa0\xa0\xa0': 0.39; 'to:addr:python.org': 0.39; 'email addr:gmail.com': 0.63; 'charset:windows-1252': 0.65; 'received:74.208': 0.68
Date Wed, 14 Jan 2015 00:49:06 +1100
From mortoxa <mortoxa@gmx.com>
User-Agent Mozilla/5.0 (X11; FreeBSD amd64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Help understanding list operatoins inside functions in python 3
References <eb7dcc11-410d-44a9-b619-e5ae70876aa1@googlegroups.com>
In-Reply-To <eb7dcc11-410d-44a9-b619-e5ae70876aa1@googlegroups.com>
Content-Type multipart/alternative; boundary="------------090809090000090308050509"
X-Provags-ID V03:K0:mcRD1yXINtkXyDbLQHxRq2RJv2Jrf90tvIj8WXNDpAVjCUnk7YB ZdoIk7ARgIfEoTI+zP1TYC9fxN0RXDO3ERCiVbPsH6vVek30d2wEFADq6HW/WbQVEodP7p0 MNXZv0DTwiZdsRdupZGYkemFnbPj0NF9jDBw5J9aWdJZ5wtPllLmDecK3J5QYjY4UP7UmAs bvKRC95ERUXZNTOweSQWw==
X-UI-Out-Filterresults notjunk:1;
X-Mailman-Approved-At Tue, 13 Jan 2015 15:24:26 +0100
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.17675.1421159068.18130.python-list@python.org> (permalink)
Lines 135
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1421159068 news.xs4all.nl 2886 [2001:888:2000:d::a6]:45637
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:83693

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On 01/13/15 23:51, stephen.boulet@gmail.com wrote:
> I'm a bit confused why in the second case x is not [1,2,3]:
>
> x = []
>
> def y():
>      x.append(1)
>
> def z():
>      x = [1,2,3]
>
> y()
> print(x)
> z()
> print(x)
>
> Output:
> [1]
> [1]

In your y() function, as you are appending data, the list must already 
exist. So the global list x is used

In your z() function, you are creating a local list x which only exists 
as long as you are in the function.
Anything you do to that list has no effect on the global list x. That is 
why the list does not change.

If you specifically wanted to change the global list x, you could do this:

def z():
     global x
     x = [1, 2, 3]

Output:
[1]
[1, 2, 3]

Or better

def z():
     x = [1, 2, 3]
     return x

y()
print(x)
x = z()
print(x)

Output:
[1]
[1, 2, 3]

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


Thread

Help understanding list operatoins inside functions in python 3 stephen.boulet@gmail.com - 2015-01-13 04:51 -0800
  Re: Help understanding list operatoins inside functions in python 3 Joel Goldstick <joel.goldstick@gmail.com> - 2015-01-13 08:39 -0500
  Re: Help understanding list operatoins inside functions in python 3 Laurent Pointal <laurent.pointal@laposte.net> - 2015-01-13 14:43 +0100
  Re: Help understanding list operatoins inside functions in python 3 mortoxa <mortoxa@gmx.com> - 2015-01-14 00:49 +1100
  Re: Help understanding list operatoins inside functions in python 3 Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-01-13 09:25 -0500

csiph-web