Python新手,有一些C&的JavaScript。
我有以下两个函数和测试用例:
def switch(a, b):
print("switch__You gave me (%d, %d)." % (a,b))
y = a
a = b
b = y
print("switch__The order is now (%d, %d)" % (a, b))
def sortHi_Low(c, d):
print("SORT You gave me (%d, %d)." % (c,d))
if (c < d):
switch(c, d)
print("SORT The order is now (%d, %d)" % (c, d))
else:
print("SORT The order did not change (%d, %d)" % (c, d))
print("Case 1 \n")
sortHi_Low(10, 3)
print("\nCase 2 \n")
sortHi_Low(4,5)
switch()
按预期工作。 sortHi_Low()
运行switch()
,但始终按照传递的顺序返回数字。
我对Python的理解是对象总是通过引用传递。当switch
将sort的变量作为参数接收时,它应该通过引用更改它们的值 - 然后sort应返回这些新值。但是,这是在解释器中打印的内容。
Case 1
SORT You gave me (10, 3).
SORT The order did not change (10, 3)
Case 2
SORT You gave me (4, 5).
switch__You gave me (4, 5).
switch__The order is now (5, 4)
SORT The order is now (4, 5)
排序应该是Hi to Low,但不起作用。