发布时间:2019-09-24 08:23:09编辑:auto阅读(2931)
******Python 不需要交换函数swap(),如果要交换a,b的话,只需要使用如下语句:
a,b = b,a
即可(因为:Python以引用方式管理对象,你可以交换引用,但通常不能交换内存中的对象值。当然你也不需要这样做。)
在python中应该这样做:
a = 1
b = 2
def swap(t1, t2):
return t2, t1
a,b = swap(a, b) # After this point, a == 2 and b == 1
But there is not way (other than abusing globals or the module
namespace) to do it like this:
不过下面这段代码不可能像我们希望的那样工作(全局命名空间和局部命名空间是隔离的):
a = 1
b = 2
def swap(t1, t2):
t2, t1 = t1, t2
return
swap(a, b)
# After this point, a == 1 and b == 2. The calling namespace is
# not changed.
上一篇: openwrt安装python整理
下一篇: Life is short,you ne
52589
52512
42600
39511
33922
30888
29669
24576
24420
22792
109°
124°
192°
190°
223°
377°
370°
376°
444°
375°