Skip to content

Altering __repr__ methods to accurately represent subclass names#270

Merged
cdgriffith merged 2 commits intocdgriffith:developfrom
gtkacz:261-fix_repr_for_subclassing
Jun 12, 2024
Merged

Altering __repr__ methods to accurately represent subclass names#270
cdgriffith merged 2 commits intocdgriffith:developfrom
gtkacz:261-fix_repr_for_subclassing

Conversation

@gtkacz
Copy link
Copy Markdown
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
Copy Markdown
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
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