cdgriffith / Box

Python dictionaries with advanced dot notation access
https://github.com/cdgriffith/Box/wiki
MIT License
2.64k stars 107 forks source link

Altering __repr__ methods to accurately represent subclass names #270

Closed gtkacz closed 5 months ago

gtkacz commented 5 months ago

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 commented 5 months ago

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!