Automatic debugging of copy by reference errors?

greg greg at cosc.canterbury.ac.nz
Mon Dec 11 06:09:48 EST 2006


Russ wrote:
> The copy by reference semantics of Python give it great
> efficiency but are also its achille's heel for tough-to-find bugs.

You need to stop using the term "copy by reference",
because it's meaningless. Just remember that assignment
in Python is always reference assignment. If you want
something copied, you need to be explicit about it.

> I later discovered that a
> particularly nasty bug was due to the fact that my constructor "copied"
> the initializing list by reference.

The reason you made that mistake was that you were
using the wrong mental model for how assignment works
in Python -- probably one that you brought over from
some other language.

When you become more familiar with Python, you won't
make mistakes like that anywhere near as often. And
if you do, you'll be better at recognising the
symptoms, so the cause won't be hard to track down.

> So a fundamental question in Python, it seems to me, is when to take
> the performance hit and use "copy" or "deepcopy."

Again, this is something you'll find easier when
you've had more experience with Python. Generally,
you only need to copy something when you want an
independent object that you can manipulate without
affecting anything else, although that probably
doesn't sound very helpful.

In your vector example, it depends on whether you
want your vectors to be mutable or immutable. It
sounds like you were treating them as mutable, i.e.
able to be modified in-place. In that case, each
vector obviously needs to be a new object with the
initial values copied into it.

The alternative would be to treat your vectors as
immutable, i.e. once created you never change their
contents, and any operation, such as adding two
vectors, produces a new vector holding the result.
In that case, two vectors could happily share a
reference to a list of values (as long as there is
nothing else that might modify the contents of the
list).

--
Greg



More information about the Python-list mailing list