Search code examples
pythonreprisinstance

I have a class with one required and two optional parameters, and a repr method which returns with one of the optional params, whichever is given


class MyClass():
    def __init__(self, name, high=None, low=None):
        self.name = name
        if low:
            self.low = low
        elif high:
            self.high = high
        else:
            raise Error("Not found")

    def __repr__(self):
        value = self.low or self.high
        return '{}({}, {})'.format(str(self), self.name, value)

I have a unit test case, in which MyClass is being instantiated like,

gain = MyClass('name', high='high_value')
assert isinstance(repr(gain), str)

But when my repr() is called, it is throwing AttributeError,

AttributeError: 'MyClass' has no attribute 'low'


Solution

  • I would refactor your code to the following

    class MyClass:
        def __init__(self, name, high=None, low=None):
            self.name = name
            self.low = low
            self.high = high
            if self.low is None and self.high is None:
                raise Error("Not found")
            if self.low is not None and self.high is not None:
                raise Error("Only low OR high may be specified, not both")
            
        def __repr__(self):
            value = self.low if self.low is not None else self.high
            return '{}({}, {})'.format(str(self), self.name, value)
    

    So in __init__ your assertion is that exactly one of low or high were set, in other words it is an error to have neither set or both set. Then in __repr__ you can assign value based on which was passed in. In this case both self.low and self.high will exist, though one of their values will be None.