Python小陷阱


MITopenCourse上Gim讲的,反正当时我是被唬住了,后来自己实验了下才明白怎么回事。问题虽然很小,但是也有收获。

这样一个判断回文的code:

[python] res = [1 , 2 , 3] 
tmp = res 
tmp.reverse() 
if(res == tmp): 
    print("number 1") 
else: 
    print("number 2") 
res = [1 , 2 , 3]
tmp = res
tmp.reverse()
if(res == tmp):
    print("number 1")
else:
    print("number 2")
会输出什么呢??? number 1 will be shown,but what's going on???(G老当时就是这么说的)

我们看下面:

 \

 

 

lol ,这么回事啊。。。其实res , tmp都是“reference”其实他们在内存中引用的是同一地址的变量,如果这么改就可以了:

 \


这个问题就好像openCV中的Mat,问题虽小,但是有所收获

the end

 

 


摘自 FishinLab的专栏

相关内容

    暂无相关文章

评论关闭