>  Can sort not modify read-only location.
>
>>>> d
> {'a': 1, 'c': 3, 'b': 2}
>
>>>> id(d)
> 412816
>
>>>> id(d.keys())
> 404296

I see why you thought d.keys() is read-only. Multiple calls to
d.keys() seems to be returning the same object.

>>> d = {'a': 1, 'c': 3, 'b': 2}
>>> id(d)
200112
>>> id(d.keys())
597448
>>> id(d.keys())
597448

However assigning it to some variable forces it to return a new object.

>>> x = d.keys()
>>> id(x)
597448
>>> id(d.keys())
594408
>>> id(d.keys())
594408
>>> y = d.keys()
>>> id(y)
594408
>>> id(d.keys())
611312

Looks like Python dictionary implementation is doing something clever.
d.keys() returns the cached object if its refcount == 1 and returns a
new object if refcount > 1.

Anand
_______________________________________________
BangPypers mailing list
[email protected]
http://mail.python.org/mailman/listinfo/bangpypers

Reply via email to