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

Altering __repr__ methods to accurately represent subclass names #270

Merged
merged 2 commits into from
Jun 12, 2024

Conversation

gtkacz
Copy link
Contributor

@gtkacz gtkacz commented Jun 7, 2024

All Box classes have their class names hardcoded into their respective __repr__ methods, making it so that when subclassing, __repr__ calls will output the original class names, which is confusing.

Example:

from box import Box


class MyBox(Box):
    pass


class ActuallyMyBox(Box):
    def __repr__(self):
        return f"{type(self).__name__}({self})"


b1 = MyBox(a=1)
b2 = ActuallyMyBox(b=2)
print(repr(b1))
print(repr(b2))

>>> Box({'a': 1})
>>> ActuallyMyBox({'b': 2})

Closes #261

@cdgriffith
Copy link
Owner

This is one of those things I swore there was a reason I couldn't do it this way, possibly a juypter notebook thing. But I am going to add this in and do some tests and hopefully good to keep it because this is a lot better!

Thank you!

@cdgriffith cdgriffith merged commit fa12326 into cdgriffith:develop Jun 12, 2024
27 of 31 checks passed
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.

2 participants