Automatic debugging of copy by reference errors?

Russ uymqlp502 at sneakemail.com
Sun Dec 10 22:44:24 EST 2006


Niels L Ellegaard wrote:
> Is there a module that allows me to find errors that occur due to copy
> by reference? I am looking for something like the following:

I don't have a solution for you, but I certainly appreciate your
concern. The copy by reference semantics of Python give it great
efficiency but are also its achille's heel for tough-to-find bugs.

I once wrote a simple "vector" class in Python for doing basic vector
operations such as adding and subtracting vectors and multiplying them
by a scalar. (I realize that good solutions exist for that problem, but
I was more or less just experimenting.) The constructor took a list as
an argument to initialize the vector. I later discovered that a
particularly nasty bug was due to the fact that my constructor "copied"
the initializing list by reference. When I made the constructor
actually make its own copy using "deepcopy," the bug was fixed. But
deecopy is less efficient than copy by reference, of course.

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

If a debugger could tell you how many references exist to an object,
that would be helpful. For all I know, maybe some of them already do. I
confess I don't use debuggers as much as I should.




More information about the Python-list mailing list