strange append

Bruno Desthuilliers onurb at xiludom.gro
Mon Oct 2 05:41:15 EDT 2006


E.Nurminski wrote:
> Hello to all good people
> 
> I am new to the great Py so am quite puzzled by the following code
> 
> ---------------
> 
> res = []
> x = [ 1, 1 ]
> for i in xrange(0,5):
> 	res.append(x)
> 	x[1] = x[1] + 1
> 	print "x = ", x
> 	print "res = ", res
> 
> ---------------
> 
> Looks like it puts smth like reference to 'x' into 'res' list, instead of 
> value. But if I want a value should I use a different method or what ?

What difference do you make between "values" and "references" ?-)

Hint 1: in Python, all you have are objects. Yes, even strings and
numbers etc...

> Evgeni
> 
> P.S. Could not easily find the issue in the manual/tutorial can you point 
> me out to smth relevant ?
> 

Hint 2 : Python has something very nice which is the interactive python
shell. This lets you try code snippets and see by yourself how it really
works :

bruno at bousin ~ $ python
Python 2.4.3 (#1, Sep 29 2006, 20:26:46)
[GCC 4.1.1 (Gentoo 4.1.1-r1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> mylist = [1, "aaa"]
>>> mylist.append(42)
>>> mylist.append("lala")
>>> mylist
[1, 'aaa', 42, 'lala']
>>>

HTH
-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list