obj2 = ojb1 = range(len(somelist))
Cromwell, Jeremy
jcromwell at ciena.com
Thu Jan 31 20:16:37 EST 2002
-----Original Message-----
From: Tom Jenkins [mailto:tjenkins at devis.com]
On Thu, 2002-01-31 at 17:28, David Bear wrote:
> I wanted to create 2 distinct lists that were derived from range
> function. rather that doing
>
> obj1 = range(len(somelist))
> obj2 = range(len(somelist))
>
> I wanted to do
>
> obj2 = ojb1 = range(len(somelist))
>
> Yet, both names point to the same object -- they're not two objects.
>
> Is there some other syntax that I could use to avoid having to call
> range(len(list)) twice and still create two objects?
i didn't think this was going to work but hey the interpreter was there
<wink>
>>> l1 = l2[:] = range(len(somelist))
>>> print l1, l2
[0, 1, 2, 3, 4] [0, 1, 2, 3, 4]
>>> print id(l1), id(l2)
135566380 135560684
>>>
--
At some point Tom must have defined l2 as a list.
Here's a listing from scratch
PythonWin 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32.
Portions Copyright 1994-2001 Mark Hammond (mhammond at skippinet.com.au) - see
'Help/About PythonWin' for further copyright information.
>>> obj2 = obj1[:] = range(10)
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
NameError: name 'obj1' is not defined
>>> obj1 = 0
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
NameError: name 'obj1' is not defined
>>> obj1 = 0
>>> obj2 = obj1[:] = range(10)
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: object does not support item assignment
>>> obj1 = []
>>> obj2 = obj1[:] = range(10)
>>> print id(obj1), id(obj2)
17584752 17586936
>>> obj1 is obj2
0
>>> print obj1, obj2
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
-Jeremy Cromwell
More information about the Python-list
mailing list