What does x[:]=[4,5,6] mean?

Erik Max Francis max at alcyone.com
Sat Jun 2 13:33:58 EDT 2001


Franz GEIGER wrote:

> In site.py I saw
> sys.path[:] = L
> What does this mean? Why is the '[:]'? Why not simply
> sy.path = L ?

It's a peculiar construct that changes the list in place, rather than
making sys.path point to a new list:

max at oxygen:~% python
Python 2.0 (#2, Apr  4 2001, 19:28:30) 
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> l = [1, 2, 3]
>>> m = l # new list that points to l, all changes to l will show up in m
>>> l[:] = [4, 5, 6] # change l in place
>>> l
[4, 5, 6]
>>> m
[4, 5, 6]
>>> l = [7, 8, 9] # point l to a new list
>>> l
[7, 8, 9]
>>> m # m still refers to the original list
[4, 5, 6]

I suspect that this is revealing an implementation detail, where the
internals of the system really would like sys.path to always point to
the same list.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Love is the triumph of imagination over intelligence.
\__/ H.L. Mencken
    REALpolitik / http://www.realpolitik.com/
 Get your own customized newsfeed online in realtime ... for free!



More information about the Python-list mailing list