simultaneous assignment

Edward Elliott nobody at 127.0.0.1
Tue May 2 16:39:09 EDT 2006


bruno at modulix wrote:
> re-phrase it according to how Python works, and you'll get the answer:
> 
> "Is there a way to bind multiple names to the same object, but so the
> identity of this object is different from the identity of this object ?"

Which raises an interesting parallel question:  is there a way to clone an
arbitrary object?  If so you could do this:

>>> a = something
>>> b = clone (a)
>>> id(a) == id(b)
False

For lists, a slice-copy creates a clone:

>>> a = [1,2,3]
>>> b = a[:]
>>> id(a) == id(b)
False


For arithmetic types, an identity operation works for large values:

>>> a = 12345
>>> b = a + 0
>>> id(a) == id(b)
False

but not small ones:

>>> a = 5
>>> b = a + 0
>>> id(a) == id(b)
True

With booleans (and None) it will never work since the interpreter only
stores one copy of each value.

I'm not saying this approach is a good idea -- value semantics are almost
always preferable, and playing with interpreter internals usually leads to
trouble.  But as an academic exercise, how close can you get to a universal
clone function?





More information about the Python-list mailing list