counting references to an instance

Gerrit gerrit at nl.linux.org
Thu Feb 5 10:23:47 EST 2004


Robert Brewer wrote:
> getrefcount(object)
> 
> Return the reference count of the object. The count returned is
> generally one higher than you might expect, because it includes the
> (temporary) reference as an argument to getrefcount().

Hmm, interesting little getrefcount test:
16:20:57:2522:gerrit at topjaklont:~/bin$ python2.3
Python 2.3.3 (#1, Feb  2 2004, 15:37:26)
[GCC 3.3.2 20031022 (Red Hat Linux 3.3.2-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> [(i,sys.getrefcount(i)-3) for i in xrange(110)]
[(0, 114), (1, 55), (2, 25), (3, 14), (4, 11), (5, 7), (6, 8), (7, 8), (8, 9), (9, 7), (10, 12), (11, 6), (12, 6), (13, 5), (14, 5), (15, 4), (16, 5), (17, 6), (18, 4), (19, 4), (20, 4), (21, 4), (22, 4), (23, 5), (24, 4), (25, 5), (26, 4), (27, 4), (28, 4), (29, 6), (30, 5), (31, 4), (32, 6), (33, 2), (34, 2), (35, 2), (36, 2), (37, 2), (38, 2), (39, 2), (40, 2), (41, 2), (42, 2), (43, 2), (44, 2), (45, 2), (46, 2), (47, 2), (48, 2), (49, 2), (50, 2), (51, 2), (52, 2), (53, 2), (54, 2), (55, 3), (56, 3), (57, 2), (58, 2), (59, 2), (60, 3), (61, 2), (62, 2), (63, 2), (64, 10), (65, 7), (66, 5), (67, 5), (68, 5), (69, 5), (70, 5), (71, 5), (72, 5), (73, 5), (74, 5), (75, 5), (76, 5), (77, 5), (78, 5), (79, 2), (80, 2), (81, 2), (82, 2), (83, 2), (84, 2), (85, 2), (86, 2), (87, 2), (88, 2), (89, 2), (90, 2), (91, 2), (92, 2), (93, 2), (94, 2), (95, 2), (96, 2), (97, 2), (98, 2), (99, 2), (100, 0), (101, 0), (102, 0), (103, 0), (104, 0), (105, 0), (106, 0), (107, 0), (108, 0), (109, 0)]
>>>
16:21:01:2523:gerrit at topjaklont:~/bin$ python2.3 -OOS
Python 2.3.3 (#1, Feb  2 2004, 15:37:26)
[GCC 3.3.2 20031022 (Red Hat Linux 3.3.2-1)] on linux2
>>> import sys; [(i,sys.getrefcount(i)-3) for i in xrange(110)]
[(0, 111), (1, 51), (2, 23), (3, 14), (4, 11), (5, 7), (6, 8), (7, 8), (8, 9), (9, 7), (10, 12), (11, 6), (12, 6), (13, 5), (14, 5), (15, 4), (16, 5), (17, 6), (18, 4), (19, 4), (20, 4), (21, 4), (22, 4), (23, 5), (24, 4), (25, 4), (26, 4), (27, 4), (28, 4), (29, 6), (30, 5), (31, 4), (32, 6), (33, 2), (34, 2), (35, 2), (36, 2), (37, 2), (38, 2), (39, 2), (40, 2), (41, 2), (42, 2), (43, 2), (44, 2), (45, 2), (46, 2), (47, 2), (48, 2), (49, 2), (50, 2), (51, 3), (52, 2), (53, 2), (54, 2), (55, 2), (56, 3), (57, 2), (58, 2), (59, 2), (60, 3), (61, 2), (62, 2), (63, 2), (64, 10), (65, 7), (66, 5), (67, 5), (68, 5), (69, 5), (70, 5), (71, 5), (72, 5), (73, 5), (74, 5), (75, 5), (76, 5), (77, 5), (78, 5), (79, 2), (80, 2), (81, 2), (82, 2), (83, 2), (84, 2), (85, 2), (86, 2), (87, 2), (88, 2), (89, 2), (90, 2), (91, 2), (92, 2), (93, 2), (94, 2), (95, 2), (96, 2), (97, 2), (98, 2), (99, 2), (100, 0), (101, 0), (102, 0), (103, 0), (104, 0), (105, 0), (106, 0), (107, 0), (108, 0), (109, 0)]

...powers of two have a lot more refences, but 17 and 29 have 2 references
more than 18 or 28... interesting.

IIRC, Python keeps references of all small integers,
apparantly all smaller than 100.

Gerrit.

-- 
PrePEP: Builtin path type
    http://people.nl.linux.org/~gerrit/creaties/path/pep-xxxx.html
Asperger's Syndrome - a personal approach:
	http://people.nl.linux.org/~gerrit/english/




More information about the Python-list mailing list