1

I am wondering if there are patterns, references or useful resources about how to implement objects that may have several equivalent representations. For example:

Suppose I want to implement a Rectangle, and I will instantiate it using its length and width:

class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

and suppose now I also want to have the possibility to instantiate the Rectangle using its diagonal and area. My main concern is that I don't want to store redundant information in the object (in this case, these four properties), only the bare minimum possible to define it.

Suppose too I might want to retrieve one set of properties having created the Rectangle using another:

r = Rectangle(length=3, width=4)
assert r.diagonal == 5

This is what I've tried so far:

class Rectangle:
    def __init__(self, **kwargs):
        params = set(kwargs.keys())
        if params == set(['length', 'width']):
            self._length = kwargs['length']
            self._width = kwargs['width']
        elif params == set(['area', 'diagonal']):
            self._area = kwargs['area']
            self._diagonal = kwargs['diagonal']
        else:
            raise ValueError('Invalid input arguments')

    @property
    def length(self):
        if hasattr(self, '_length'):
            return self._length
        elif hasattr(self, '_area') and hasattr(self, '_diagonal'):
            length, width = ad2lw(self._area, self._diagonal)
            #r = Rectangle(length=length, width=width)
            #return r._length
            return length
        else:
            raise NotImplementedError('No converter available')

    # Same with other three properties


def ad2lw(area, diagonal):
    # Obscure math
    return l, w

Is this any known pattern or is there a standard way to do it?

1 Answer 1

3

The whole idea is that you have object defined in one way, eg. using width and length and you can calculate other parameters, eg. diagonal and area from them.

First your constructor/init only accepts length and width, but the class could also have static createFromDiagonal method, that accept diagonal and area, calculates the width and length, creates the instance using those parameters and returns this instance.

The second thing is accessing the values. You can easily access length and width, because those are what defines the object. And for diagonal and area you can use Properties, which will calculate the required value from length and width while access from outside will look like normal field.

3
  • Thanks for your answer, I see you consider one of the representations as the canonical one. I'll think about this approach. Thanks! Apr 6, 2014 at 20:23
  • @Juanlu001 Thats basically it. But the real problems starts when converting from one representation to another is computationally expensive. In which case, some kind of change tracking is required so you don't needlesly recalculate it every call.
    – Euphoric
    Apr 6, 2014 at 20:26
  • @Juanlu001 In addition to the canonical version, there's one special case along similar lines. Sometimes the "other properties" of the object are actually simply parts of the canonical representation itself. In this case it may be possible to leverage a union and/or bitfield. The textbook example of this is the concept of a pixel with RGBA components being just as useful as the value as a uint32. However, this is a very narrow case, and I would consider it to be part of this "canonical form" version.
    – J Trana
    Apr 7, 2014 at 3:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.