Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #83687 > unrolled thread
| Started by | stephen.boulet@gmail.com |
|---|---|
| First post | 2015-01-13 04:51 -0800 |
| Last post | 2015-01-13 09:25 -0500 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.python
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
| From | stephen.boulet@gmail.com |
|---|---|
| Date | 2015-01-13 04:51 -0800 |
| Subject | Help understanding list operatoins inside functions in python 3 |
| Message-ID | <eb7dcc11-410d-44a9-b619-e5ae70876aa1@googlegroups.com> |
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]
[toc] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2015-01-13 08:39 -0500 |
| Message-ID | <mailman.17672.1421156389.18130.python-list@python.org> |
| In reply to | #83687 |
[Multipart message — attachments visible in raw view] — view raw
On Tue, Jan 13, 2015 at 7:51 AM, <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] > x in the outer scope is not x in the z() scope > -- > https://mail.python.org/mailman/listinfo/python-list > -- Joel Goldstick http://joelgoldstick.com
[toc] | [prev] | [next] | [standalone]
| From | Laurent Pointal <laurent.pointal@laposte.net> |
|---|---|
| Date | 2015-01-13 14:43 +0100 |
| Message-ID | <54b52101$0$12756$426a34cc@news.free.fr> |
| In reply to | #83687 |
Hello,
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]
Here x is a local, so global x is not modified.
If you want to modify gobal x, write:
def z():
global x
x = [1,2,3]
You can read the Python FAQ about global/local rules:
https://docs.python.org/3/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python
Or Dive Into Python
http://www.diveintopython.net/html_processing/locals_and_globals.html
(and for your problem, its the same with Python2 and Python3)
Or…
A+
Laurent.
[toc] | [prev] | [next] | [standalone]
| From | mortoxa <mortoxa@gmx.com> |
|---|---|
| Date | 2015-01-14 00:49 +1100 |
| Message-ID | <mailman.17675.1421159068.18130.python-list@python.org> |
| In reply to | #83687 |
[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]
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2015-01-13 09:25 -0500 |
| Message-ID | <mailman.17676.1421159409.18130.python-list@python.org> |
| In reply to | #83687 |
On Tue, 13 Jan 2015 04:51:19 -0800 (PST), stephen.boulet@gmail.com
declaimed the following:
>I'm a bit confused why in the second case x is not [1,2,3]:
>
>x = []
>
>def y():
> x.append(1)
MODIFY contents of object identified by the name "x"; "x" is not a
local name, search module (global) level to find it.
>
>def z():
> x = [1,2,3]
Create an anonymous list containing three elements; attach the LOCAL
name "x" to that list; never looks at module "x:
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web