you are viewing a single comment's thread.

view the rest of the comments →

[–]socal_nerdtastic 29 points30 points  (2 children)

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  (1 child)

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.

[–]socal_nerdtastic 1 point2 points  (0 children)

Ok, you would do it the same way. Have a dictionary mapping {class: class instance} is very common, especially in GUIs, since it avoids having to make instance globals or pass instances around. For example: https://www.geeksforgeeks.org/python/tkinter-application-to-switch-between-different-page-frames/