"sys.getrefcount ()" return value

Why

sys.getrefcount() 

return 3 for every large number or prime string? Does this mean that 3 objects are located somewhere in the program? Also, why does setting x = (a very large number) not increase the number of objects? the result of my getrefcount call? Thanks for clarifying this.

eg:

>>> sys.getrefcount(4234234555)
3
>>> sys.getrefcount("testing")
3
>>> sys.getrefcount(11111111111111111)
3
>>> x=11111111111111111
>>> sys.getrefcount(11111111111111111)
3 
+5
source share
2 answers

Large integer objects are not reused by the interpreter, so you get two different objects:

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

sys.getrefcount (11111) always returns the same number, since it measures the number of references to a new object.

For small integers, Python always uses the same object:

>>> sys.getrefcount(1)
73

Usually you should get only one link to a new object:

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

Python , , - .

C : http://svn.python.org/view/python/trunk/Objects/intobject.c?view=markup

: , , , , , :

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

  • Python . getrefcount('foobar') getrefcount('foo' + 'bar'). ( 'foo' 'bar'.)

  • :

, , , , () getrefcount().

+6

All Articles