Tuple slices

Pedro Werneck pedro.werneck at terra.com.br
Mon Jan 24 13:19:13 EST 2005


On Mon, 24 Jan 2005 18:45:46 +0100
"Fredrik Lundh" <fredrik at pythonware.com> wrote:

> George Sakkis wrote:
> 
> > Why does slicing a tuple returns a new tuple instead of a view of
> > the existing one, given that tuples are immutable ?
> 
> really?

Well... seems like this case is optimized to return the original tuple
just incrementing its reference count and returning

tupleobject.c, 330-335

        if (ilow == 0 && ihigh == a->ob_size && PyTuple_CheckExact(a)) {
                Py_INCREF(a);
                return (PyObject *)a;
	}


> 
> >>> a = 1, 2, 3
> >>> b = a[:]
> >>> a is b
> True
> 
> </F> 
> 
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list