[BangPypers] sort query

bhaskar jain bhaskar.jain2002 at gmail.com
Sat Oct 24 20:19:18 CEST 2009

>>>On Sat, Oct 24, 2009 at 5:09 PM, Sidharth Kuruvila <
sidharth.kuruvila at gmail.com> wrote:
>>>So do  a lot of hard thinking before you say something is broken.

I have never questioned the correctness, rather wanted to clear my doubt.

>>> d = {'a':1, 'b':2, 'c':3}

>>> id(d.keys())

>>> d.keys().sort()

>>> id(d.keys())

>>> a = d.keys()
>>> print a
['a', 'c', 'b']

>>> id(a)

What did sort do?

>>>Now there is another subtle issue here with d.keys(), ever time you
>>>call it, you get a new list object, not the one you got with the
>>>previous call

I taught myself to believe that "Python returns references  to the same
objects it already had around".
So if i called dict.keys() and have *not* modified my dict and then call
dict.keys() again, i should get a reference to the earlier created list
Anyways it is not so.

Thanks anyways,

More information about the BangPypers mailing list