events icon indicating copy to clipboard operation
events copied to clipboard

Add population of `__dict__` variable with `__events__`, if present.

Open CaileanMParker opened this issue 1 year ago • 0 comments

I added the population of the __dict__ variable with __events__ (if present), when the Events class is instantiated.

I did this as I noticed an issue where events could be accessed via strings (e.g., events["on_my_event"]) only if they had previously been accessed directly (i.e., though __getattr__), which added them to the __dict__ variable. This fix works both for cases of direct Events class instantiation (e.g., events = Events(("on_one", "on_two")) and subclassing (e.g.,

class MyEvents(Events):
    __events__ = ("on_one", "on_two")

).

Note, the new __dict__ variable is generated in the way it is (rather than just in a for loop) for two reasons:

  1. timeit indicated it was (albeit only very slightly) faster
  2. This method is safer in that it will never overwrite a key in the original dictionary if there happens to be a collision.

CaileanMParker avatar Aug 07 '23 21:08 CaileanMParker