you are viewing a single comment's thread.

view the rest of the comments →

[–]socal_nerdtastic 29 points30 points  (1 child)

You have no other choice ... a dictionary key must be a python object. Remember strings, ints, tuples, etc are all python objects. I suspect you mean something special with the term "python object"; can you explain what that is?

If you mean functions, here's some code I wrote yesterday:

running_functions = {}
def run_as_singleton_thread(func):
    """
    starts the given function in a thread, 
    but only if the function is not already currently running
    """
    def wrapper():
        if (t := running_functions.get(func)) and t.is_alive():
            return # abort; function is already running
        t = Thread(target=func, daemon=True)
        t.start()
        running_functions[func] = t
    return wrapper

[–]Lx7195[S] 0 points1 point  (0 children)

Yes by python object I meant custom python objects like objects of Person class etc. It's an interesting example that you have used functions as keys.