I have an abstract base class representing an interface. Subclasses of this class are stored as properties of other subclasses of this class.
For instance:
class AbstractBase(object):
pass
class Child(AbstractBase):
def __init__(self, cls1, cls2):
assert isinstance(cls1, AbstractBase)
assert isinstance(cls2, AbstractBase)
self.cls1 = cls1
self.cls2 = cls2
The depth and location of the hierarchy cannot be known in advance, but will not be recursive.
What can I put as __repr__on AbstractBase, which will allow me to find the useful properties of each child class in a useful way?
My current approach:
from pprint import pformat
class AbstractBase(object):
def __repr__(self):
return self.__class__.__name__ + '\n' \
+ pformat({k:v for k,v in self.__dict__.iteritems()
if not '__' in k})
For a base class (without properties that are subclasses AbstractBase), this outputs something readable, for example:
MyClass
{'var1': 1,
'var2': 2}
AbstractBase , , , (, __repr__ ).
- , cls1 cls2 int var:
Child
{'cls1': {
'var': 1,
},
'cls2': {
'var': 0,
}
}
, , ( ). ?