On 14/03/2016 21:00, Christian Gollwitzer wrote:
Am 14.03.16 um 21:31 schrieb BartC:
There are good reasons for wanting to do so. Try writing this function
in Python:
def swap(a,b):
b,a = a,b
x="one"
y="two"
swap(x,y)
print (x,y)
so that it displays "two" "one".
The pervert thing is that this is nearly there:
def swap(a,b):
c=[]
c.append(*a)
a[:]=b[:]
b[:]=c[:]
x=["one"]
y=["two"]
swap(x,y)
print x
print y
The list thing I've already tried. Although you've made it swap() more
complicated than it need be. I used
def swap(a,b):
b[0],a[0]=a[0],b[0]
(Perhaps yours swapped the entire lists not just the first element? But
that didn't work when I tried your code.)
The problem is that in general, x and y can be anything; maybe x is an
element of a list, y is tuple. Even if by chance they were lists, then
you'd want the entire list swapped.
Now with a similar example, I had created a bug some time ago.
It looks like you created a feature not a bug...
--
Bartc
--
https://mail.python.org/mailman/listinfo/python-list