string slicing

Fredrik Lundh fredrik at pythonware.com
Sun Dec 5 11:07:47 EST 2004


Ishwor wrote:

>  I am trying some interactive examples here where i have come across
> inconsistencies??? :O

obsession with implementation artifacts is a premature optimization,
and should be avoided.

> Anyway heres whats bothering me
>
>>>> s = 'hello'
>>>> s[0]
> 'h'
>>>> s[:]
> 'hello'
>>>> m = s[:]
>>>> m
> 'hello'
>>>> m is s
> True
>
> I discussed the *is* operator with some of the pythoners before as
> well but it is somewhat different than what i intended it to do. The
> LP2E by Mark & David says -
> " m gets a *full top-level copy* of a sequence object- an object with
> the same value but distinct piece of memory." but when i test them
> with *is* operator then the result is True. Why is this happening??

since you cannot modify a string in place, the string implementation can
safely return the original string as the "full copy".  "mutable" objects (such
as lists and arrays) cannot cheat.

a suggestion: if you really want to be productive in python, forget about
"is" for a while.  good code doesn't copy stuff much, either, by the way.
python's all about objects, and things that hold references to objects.

</F> 






More information about the Python-list mailing list