python语言中三个奇妙的返回值

从公众号看到的,记录下

d = {}
d[5] = "test1"
d[5.0] = "test2"
d["5"] = "test3"

print(d[5])
print(d[5.0])
print(d["5"])


print(5 == 5.0)

print(hash(5) == hash(5.0))


# python字段的key值是通过判定值是否相等和比较hash来判定两个键值是否相等

# test2
# test2
# test3
# True
# True



def test():
    try:
        return "fun"
    finally:
        return "try"

res = test()
print(res)
# try


# 无论怎样 finally一定会执行,所以函数的返回值是最后一个return返回的值

class test(object):
    def __init__(self):
        print("i")

    def __del__(self):
        print("d")


print(test()  == test())


print(id(test()) == id(test()))


# i
# i
# d
# d
# False
# i
# d
# i
# d
# True

 

你可能感兴趣的:(python语言中三个奇妙的返回值)