Scope troubles with a swap function
Matthew D. Wood
woodm at equire.com
Fri Aug 10 10:09:51 EDT 2001
Ok, I've been banging my head against this for a sufficiently long time
that I feel justified in presenting this personal challenge to the list.
How do you make a swap function?
Everything in python is a reference, but creating a function that will
swap the references is quite difficult (at least for me it is).
How do you make a function or a callable object instance that will do
what the following code intends to do:
def swap (heaven, hell) :
purgatory = heaven
heaven = hell
hell = purgatory
In C++ this was a VERY easy function to write because you could pass
things via reference; however, I don't want to write some sort of C
extension for something as simple as swapping 2 variables.
I have been trying to abuse some sort of meta-variable, like __dict__ to
do this, but I have done nothing more than confuse myself. That and
there is a lingering fear in my mind that if one or more of the
to-be-swapped variables are in __methods__ or __members__ dicts, then I
will definitely be lost.
That and if the __dict__, __methods__, __members__ approach is used, how
does one deal with scoping?
swap(g.h.var, a.q.member)
Thus, I present this problem to the list.
More information about the Python-list
mailing list