You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A common pattern for me is to subclass Box to get all of its features, but with a type custom to the application. Because box hard codes the types in the __repr__ method for each class, failed test reports and print debugging is often confusing. Where I am expecting to see MyCustomBox(...), instead just Box(...) is printed which makes me think I incorrectly instantiated an object.
This can be fixed by using type(self).__name__ instead of hard coding each type name within the class. It also has the benefit that duplicate code could be removed from some builtin Box subclasses, such as SBox because it would be dynamically generated from the base Box class.
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})
The text was updated successfully, but these errors were encountered:
* Adding #266 support for accessing nested items in BoxList using numpy-style tuple indexing (thanks to Bit0r)
* Adding tests and Cython releases for Python 3.12
* Fixing #251 support for circular references in lists (thanks to Muspi Merol)
* Fixing #261 altering all `__repr__` methods so that subclassing will output the correct class name (thanks to Gabriel Tkacz)
* Fixing #267 Fix type 'int' not iterable (thanks to YISH)
---------
Co-authored-by: Bit0r <nie_wang@outlook.com>
Co-authored-by: Muspi Merol <me@promplate.dev>
Co-authored-by: Gabriel Tkacz <55806524+gtkacz@users.noreply.github.com>
Co-authored-by: Gabriel Tkacz <gabriel.tkacz@gscap.com.br>
Co-authored-by: YISH <mokeyish@hotmail.com>
A common pattern for me is to subclass
Box
to get all of its features, but with a type custom to the application. Because box hard codes the types in the__repr__
method for each class, failed test reports and print debugging is often confusing. Where I am expecting to seeMyCustomBox(...)
, instead justBox(...)
is printed which makes me think I incorrectly instantiated an object.This can be fixed by using
type(self).__name__
instead of hard coding each type name within the class. It also has the benefit that duplicate code could be removed from some builtin Box subclasses, such asSBox
because it would be dynamically generated from the baseBox
class.Example:
The text was updated successfully, but these errors were encountered: