Strange behavior with classes deriving from object and weakrefs.
Duncan Booth
duncan at NOSPAMrcp.co.uk
Thu Aug 1 05:16:31 EDT 2002
tweedgeezer at hotmail.com (Jeremy Fincher) wrote in
news:698f09f8.0207311155.4fdcb159 at posting.google.com:
>>>> d.items()
> [('O', <class '__main__.O'>)]
>>>> sys.getrefcount(d['O'])
> 5
>
> Whence came those 5 references?
One of them came from the line above it. Typing a value at the interactive
prompt assigns the result to '_', so when you enter the getrefcount again,
you get one reference fewer:
>>> d.items()
[('O', <class '__main__.O'>)]
>>> sys.getrefcount(d['O'])
5
>>> sys.getrefcount(d['O'])
4
>>>
Another reference comes from the expression d['O'] which converts the weak
reference to a strong reference. So there are actually only 3 references
unaccounted for. All of these actually appear as soon as you declare the
class:
>>> class O(object): pass
>>> sys.getrefcount(O)
5
>>>
(3 unknown, plus O + the getrefcount argument reference)
There aren't any references from object.__subclasses__(), that uses weak
references until you actually call it.
I think one of the references comes from O.__mro__, that definitely creates
a cycle though it should have been handled by the garbage collector if it
were the only one.
And then there were two... but I can't spot them.
--
Duncan Booth duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
More information about the Python-list
mailing list