dreamlike-zzg

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

python 万物皆对象,数字也不例外

def fun(p):
    p = p  # nothing be changed
    print("before:",id(p))
    p += 1 # "2" be created
    print("after :",id(p))
    print(p)


a = 1  # "1" be created
b = 1  # no more "1" be created
fun(a)
print(id(a))

print(id(b))
b += 1
print(id(b))
b -= 1
print(id(b))
OUTPUT
before: 139971409287872
after : 139971409287904
2
139971409287872

139971409287872
139971409287904
139971409287872

139971409287872 139971409287904

int类型的对象“1”、“2”仅仅被创建一次

posted on 2022-11-05 15:59  梦幻济公  阅读(22)  评论(0编辑  收藏  举报