The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)
Christian Gollwitzer
auriocus at gmx.de
Mon Mar 14 17:00:51 EDT 2016
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
Now with a similar example, I had created a bug some time ago. Try:
import numpy
def twice(x):
x*=2
a=5
b=numpy.array(5)
twice(a); twice(b)
print a
print b
This is actually one of the warts in Python. Yes there are some
explanations with boxes and arrows and sticky notes and mutable and
immutable values... but actually you understand it best if you know how
CPython works and that a list is passed as a pointer.
Christian
More information about the Python-list
mailing list