Pregunta

¿Cómo se crea una referencia débil a un objeto en Python?

¿Fue útil?

Solución

>>> import weakref
>>> class Object:
...     pass
...
>>> o = Object()
>>> r = weakref.ref(o)
>>> # if the reference is still active, r() will be o, otherwise None
>>> do_something_with_o(r()) 

Ver el documentos del módulo wearkref para más detalles.También puedes usar weakref.proxy para crear un objeto que represente o.tirará ReferenceError si se usa cuando ya no se hace referencia al referente.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top