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

Why are instance attributes initialized with self.__dict__? #56

Closed
Seanny123 opened this issue Jan 31, 2022 · 3 comments · Fixed by #67
Closed

Why are instance attributes initialized with self.__dict__? #56

Seanny123 opened this issue Jan 31, 2022 · 3 comments · Fixed by #67

Comments

@Seanny123
Copy link
Contributor

Less of an issue, more of a curiosity.

In all the class __init__(self) methods, there is something like:

self.__dict__["chars"]   = chars
self.__dict__["negated"] = negateMe

Instead of:

self.chars   = chars
self.negated = negateMe

This breaks static checks, but since this is mostly your repository I don't think that's a problem. However, I am curious why you initialize attributes this way. Does it give some special functionality that I missed?

@qntm qntm added the question label Feb 1, 2022
@qntm
Copy link
Owner

qntm commented Feb 1, 2022

This is going back a way, but I believe my objective was to make it so that the objects created by greenery were immutable - to prevent consumers from making manual changes to obj.chars or obj.negated. There are probably better ways of achieving the same result that I wasn't aware of at the time.

@Seanny123
Copy link
Contributor Author

FYI, the modern (Python 3.7) approach seems to be marking a class with @dataclass(frozen=True).

To be clear, I do not expect you to make these changes to the code base! If you're curious about how this would look, I can make an Merge Request.

@qntm
Copy link
Owner

qntm commented Feb 5, 2022

If you opened a PR which made that change, I would probably respond positively 👍

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

Successfully merging a pull request may close this issue.

2 participants