Why does sys.getrefcount give huge values?

前端 未结 2 1353
天涯浪人
天涯浪人 2021-01-21 06:20
import sys

a = 10
b = a
print sys.getrefcount(a)
b=1
print sys.getrefcount(b)

output:

22 614

Is there any problem with my

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-21 06:33

    You can see the effects of Python's optimizations (see @Nether's answer) if you change the values to non-trivial ones. For instance, if your code is changed to:

    from __future__ import print_function
    
    import sys
    
    a = 1012345
    b = a
    print(sys.getrefcount(a))
    b=12345678
    print(sys.getrefcount(b))
    

    The output is:

    5
    4
    

提交回复
热议问题