Method to know if object support being weakreferenced ?

Steven D'Aprano steve+comp.lang.python at
Tue Dec 13 01:00:02 EST 2016

On Tuesday 13 December 2016 15:57, Matthias Bussonnier wrote:

> I could of course write a function that try/except and return False/True
> depending on the result, but that seem suboptimal as how can I know that the
> TypeError does come from not being able to take a weak reference ? And not
> from something else ?

Do you mean something like this?

    proxy = weakref.proxy(obj)
except TypeError:
    proxy = None

Where else could it come from?

> The common Idiom in CPython, at the C layer seem to be
> PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob)). So I can (and did) write a C-extension
> that expose such a function, but it does seem silly that no one else did that
> before me, and that no one seemed to have encountered the problem before.
> So am I missing something ? Is such a function not useful ?  Is there any
> reason not to have it in the stdlib ?

You could try raising a feature request on the bug tracker.

"Ever since I learned about confirmation bias, I've been seeing 
it everywhere." - Jon Ronson

More information about the Python-list mailing list