栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

“sys.getrefcount” return value

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

“sys.getrefcount” return value

Large integer objects are not reused by the interpretor, so you get two
distinct objects:

>>> a = 11111>>> b = 11111>>> id(a)40351656>>> id(b)40351704

sys.getrefcount(11111) always returns the same number because it measures the
reference count of a fresh object.

For small integers, Python always reuses the same object:

>>> sys.getrefcount(1)73

Usually you would get only one reference to a new object:

>>> sys.getrefcount(object())1

But integers are allocated in a special pre-malloced area by Python for
performance optimization, and I suspect the extra two references have
something to do with this.

You can look at the C implementation here:
http://svn.python.org/view/python/trunk/Objects/intobject.c?view=markup

Edit: I do not claim to understand what’s going on in lowlevel details, I
think there are several things at work that cache temporary references:

print sys.getrefcount('foo1111111111111' + 'bar1111111111111') #1print sys.getrefcount(111111111111 + 2222222222222) #2print sys.getrefcount('foobar333333333333333333')   #3


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/376111.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号