anything like C++ references?

Donn Cave donn at drizzle.com
Sun Jul 13 15:24:14 EDT 2003


Quoth Stephen Horne <intentionally at blank.co.uk>:
| On 13 Jul 2003 12:19:08 -0400, aahz at pythoncraft.com (Aahz) wrote:
|
|> Whether one can mutate a specific object is simply an
|> attribute of that object, rather than requiring a different syntax.
|> Trying to focus on the mutable/immutable distinction is what causes the
|> mental blowup -- keep your eye on the objects and bindings and you're
|> fine.
|
| That's exactly it - you have to focus on whether an object is mutable
| or immutable for that exact reason.
|
| While I admit I'm not sure, I believe that in early versions of Python
| immutable values literally were copied. Mutable types were made
| mutable because of efficiency concerns about copying overheads that
| would occur otherwise. If an object is large enough that it is
| worthwhile modifying items within it in-place then it tends to be
| worthwhile trying to avoid copying overheads, even though these two
| aspects of 'mutability' are actually quite distinct in principle.

Sure, mutable values literally are copied today.  If you have a tuple
of time.localtime(x), for example, and you want that value but with
tm_mday incremented, then you must copy the tuple to do that (except
for tm_mday, for which you substitute your own value.)  It is indeed
inefficient - OK for this purpose but not for large sequences, and
there we tend to prefer a mutable list.

That's the efficiency argument.  Assigment never copied values, in
any version of Python that anyone cared about.  We just don't want
big immutable arrays that have to be copied just to change a single
value.  There isn't a tuple analogue for the dictionary because it
wouldn't be so generally useful (the recently implemented struct
tuple would have been nice to have earlier, though.)

He's quite right, the notion of mutability is a red herring.  It
isn't that no one cares - the propagation of state changes in a
program is critical to the operation of the program, and to reasoning
about how the program will operate.

It's just that when you're trying to come to grips with how assignment
and argument passing works in Python, you don't want to have people
start explaining things in these terms (``see, this object is mutable
and that one isn't''), because you will understandably but incorrectly
apply that to assignment and argument passing.  Mutability in Python
has no effect on assignment and argument passing, it is just an attribute
of the passed or assigned object.

	Donn Cave, donn at drizzle.com




More information about the Python-list mailing list