More random python observations from a perl programmer
m.faassen at vet.uu.nl
Sun Aug 22 02:57:15 CEST 1999
Tom Christiansen <tchrist at mox.perl.com> wrote:
> [courtesy cc of this posting mailed to cited author]
> It's because @a is a first-class array, and $a merely a reference
> thereto. Copying things not producing a copy is well, not what
> non-serious programmers immediately think of. Why can python copy
> sequences of chars (1-byte strings) trivially:
> s = "this stuff"
> t = s
> But as soon as you have a sequence of, oh, integers for example, you
> can't use the same set-up. Very odd.
Perhaps you misunderstand this. Python does not copy strings. Python only
makes a new reference to that string. But since that string is
immutable, it feels exactly the same as making a copy. But no actual
copy is made. Making a new reference to a completely immutable object is
functionally identical to making a copy of it.
The gotcha is this one: a tuple is an immutable object, but it can *contain*
mutable objects. So this rule doesn't apply for all tuples. But it does
for all strings.
More information about the Python-list