Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add population of __dict__ variable with __events__, if present. #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

CaileanMParker
Copy link

@CaileanMParker CaileanMParker commented Aug 7, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant