counting references to an instance

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Fri Feb 6 00:08:41 CET 2004


> From: Gerrit
>
> ...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.

Actually -1 through to 99.

To understand why the various integers have various counts, have a look through the modules loaded at startup. Note the differences on my machine between 'python' and 'python -S' ...

C:\Documents and Settings\timothyd>c:\Python23\python.exe
Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys; [(i,sys.getrefcount(i)-3) for i in xrange(-1, 110)]
[(-1, 16), (0, 65), (1, 55), (2, 37), (3, 20), (4, 19), (5, 10), (6, 6), (7, 7),
 (8, 12), (9, 6), (10, 13), (11, 7), (12, 6), (13, 8), (14, 5), (15, 7), (16, 10
), (17, 5), (18, 5), (19, 6), (20, 6), (21, 7), (22, 7), (23, 8), (24, 5), (25,
5), (26, 5), (27, 5), (28, 5), (29, 5), (30, 5), (31, 5), (32, 9), (33, 5), (34,
 5), (35, 5), (36, 5), (37, 6), (38, 5), (39, 5), (40, 5), (41, 5), (42, 5), (43
, 5), (44, 5), (45, 5), (46, 5), (47, 5), (48, 5), (49, 5), (50, 5), (51, 5), (5
2, 5), (53, 5), (54, 5), (55, 6), (56, 6), (57, 5), (58, 5), (59, 5), (60, 5), (
61, 5), (62, 5), (63, 5), (64, 10), (65, 6), (66, 5), (67, 5), (68, 5), (69, 5),
 (70, 5), (71, 5), (72, 6), (73, 5), (74, 5), (75, 5), (76, 5), (77, 5), (78, 5)
, (79, 5), (80, 5), (81, 5), (82, 5), (83, 5), (84, 5), (85, 5), (86, 5), (87, 5
), (88, 5), (89, 5), (90, 5), (91, 5), (92, 5), (93, 5), (94, 5), (95, 5), (96,
5), (97, 5), (98, 5), (99, 5), (100, 0), (101, 0), (102, 0), (103, 0), (104, 0),
 (105, 0), (106, 0), (107, 0), (108, 0), (109, 0)]
>>>

C:\Documents and Settings\timothyd>c:\Python23\python.exe -S
Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)] on win32
>>> import sys; [(i,sys.getrefcount(i)-3) for i in xrange(-1, 110)]
[(-1, 10), (0, 45), (1, 35), (2, 22), (3, 11), (4, 11), (5, 3), (6, 2), (7, 3),
(8, 8), (9, 2), (10, 8), (11, 5), (12, 1), (13, 3), (14, 1), (15, 3), (16, 5), (
17, 1), (18, 1), (19, 1), (20, 1), (21, 3), (22, 4), (23, 3), (24, 1), (25, 1),
(26, 1), (27, 1), (28, 1), (29, 1), (30, 1), (31, 1), (32, 5), (33, 1), (34, 1),
 (35, 2), (36, 1), (37, 1), (38, 1), (39, 1), (40, 1), (41, 1), (42, 1), (43, 1)
, (44, 1), (45, 2), (46, 1), (47, 1), (48, 1), (49, 1), (50, 1), (51, 1), (52, 1
), (53, 1), (54, 1), (55, 1), (56, 2), (57, 1), (58, 1), (59, 1), (60, 1), (61,
1), (62, 1), (63, 1), (64, 6), (65, 1), (66, 1), (67, 1), (68, 1), (69, 1), (70,
 1), (71, 1), (72, 1), (73, 1), (74, 1), (75, 1), (76, 1), (77, 1), (78, 1), (79
, 1), (80, 1), (81, 1), (82, 1), (83, 1), (84, 1), (85, 1), (86, 1), (87, 1), (8
8, 1), (89, 1), (90, 1), (91, 1), (92, 1), (93, 1), (94, 1), (95, 1), (96, 1), (
97, 1), (98, 1), (99, 1), (100, 0), (101, 0), (102, 0), (103, 0), (104, 0), (105
, 0), (106, 0), (107, 0), (108, 0), (109, 0)]
>>>

Tim Delaney




More information about the Python-list mailing list