Search code examples
pythonkivyweak-references

kivy compare to weakref


How do I compare if a kivy weakref points to a given object?

Setup:

>>> my_obj
<__main__.Marker object at 0x610054ee8> 

>>> my_list
[<weakref at 0x6100662c8; to 'Marker' at 0x610054ee8>]

My list is concretely touch.grab_list comming from a kivy touch event and contains weakrefs to all grabbed widgets.

I would like to do something like if my_obj in my_list: and get True but clearly this returns False because the weakref and my_obj are different objects. What would be the correct approach?


Solution

  • Sorry, I feel stupid now, just stumbled across the solution.

    A weakref returns the object when called. So we can use that and map:

    >>> my_obj in map(lambda x: x(), my_list)
    True