How to isolate a constant?

Ian Kelly ian.g.kelly at gmail.com
Tue Oct 25 20:30:32 EDT 2011


On Tue, Oct 25, 2011 at 6:08 PM, Dennis Lee Bieber
<wlfraed at ix.netcom.com> wrote:
> Where's the line form to split those who'd prefer the first vs the
> second result in this sample <G>:
>
>>>> unExpected = "What about a string"
>>>> firstToLast = unExpected[:]

Strings are immutable.  That doesn't suffice to copy them, even
assuming you would want to do so in the first place.

>>> unExpected is firstToLast
True

If you find yourself needing to make a copy, that usually means that
you plan on modifying either the original or the copy, which in turn
means that you need a type that supports modification operations,
which usually means a list.  If you pass in a string and then copy it
with [:] and then try to modify it, you'll get an exception.  If you
don't try to modify it, then you probably didn't need to copy it in
the first place.

Cheers,
Ian



More information about the Python-list mailing list