def func(x):
print "inside function" ,id(x)
x = 2
x = 50
print "outside function" ,id(x)
print 'Value of x before function call is', x
func(x)
print 'Value of x after function call is', x
输出:
outside function 6486996
Value of x before function call is 50
inside function 6486996
Value of x after function call is 50
假设id()
给出对象的内存位置。即使两者都保存在同一位置,如果 x 值在 中更改func()
,则不会在外部影响。