I've created a class that can be initialized in different ways. Depending on the way it is initialized, it will either have one attribute (data) or two others (inputs, outputs) and some optional ones. What's the simplest way to write the repr method to handle all possibilities more generally?
Here is my first attempt based on this advice:
def __repr__(self):
# Compose a string representation of the object
s = []
try:
if self.data is not None:
s.append("data=%s" % self.data.__repr__())
except AttributeError:
s.append("inputs=%s" % self.inputs.__repr__())
s.append("outputs=%s" % self.outputs.__repr__())
try:
if self.name is not None:
s.append("name=%s" % self.name.__repr__())
except AttributeError:
pass
return "MLPTrainingData(" + ", ".join(s) + ")"
I'm guessing this is a common problem and there might be a better way...