[issue20135] mutate list

2014-01-05 Thread Xiaoqing Rong

New submission from Xiaoqing Rong:

I was using IDLE (Python GUI) for Python 3.3.3. I don't know if this is a bug 
or you guys intend it to be this way:

 x=[1,2]
 y=x
 y.append(3)
 x
[1, 2, 3]

personally i'd prefer x stays as [1,2] when i'm trying to mutate y

also:
 def f1(m,n=['haha']):
if m==n:
print('m==n')
else:
print('m!=n')
n.append('yaya')

 f1(['haha'])
m==n
 f1(['haha'])
m!=n

I'd prefer getting consistent results when calling functions like f1

--
messages: 207410
nosy: m123orning
priority: normal
severity: normal
status: open
title: mutate list
type: behavior
versions: Python 3.3

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue20135
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue20135] mutate list

2014-01-05 Thread R. David Murray

R. David Murray added the comment:

Both of these are FAQs, but to my surprise there don't seem to be answers to 
them in the FAQ list.  We should add some.

Yes, both of these behaviors is intentional.  The first is an important part of 
the language design: 'variable' names are just pointers to objects, so x and y 
point to the same object.  The second is the same issue combined with another 
design feature: the objects on the right side of the = in a def statement are 
evaluated/created at the time the def statement is executed, not at the time 
the function is called.  So 'n' points to the same object every time the 
function is called.

I'm going to leave this open until someone either points me to the FAQ entries 
I missed, or we add them.

--
assignee:  - docs@python
components: +Documentation
nosy: +docs@python, r.david.murray
versions: +Python 2.7, Python 3.4

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue20135
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com