Solution 1

``````class Weight(object):
def __init__(self, kilograms=None, pounds=None):
if not any([kilograms, pounds]):
raise ValueError('Provide at least one weight value')

self._kilograms = kilograms
self._pounds = pounds

@property
def kilograms(self):
return self._kilograms or self._pounds / 2.2

@property
def pounds(self):
return self._pounds or self._kilograms * 2.2

def __add__(self, other):
if self._kilograms:
return Weight(
kilograms=(self.kilograms + other.kilograms))
return Weight(pounds=(self.pounds + other.pounds))
``````

Solution 2

``````class Weight(object):
def __init__(self, kilograms=None, pounds=None):
if not any([kilograms, pounds]):
raise ValueError('Provide at least one weight value')

self._kg = kilograms
self._lb = pounds

@property
def kilograms(self):
if self._kg is not None:
return self._kg
return self._lb * 0.453592

@property
def pounds(self):
if self._lb is not None:
return self._lb
return self._kg * 2.20462

def __add__(self, other):
if self._kg is not None:
return Weight(kilograms=(self.kilograms + other.kilograms))
return Weight(pounds=(self.pounds + other.pounds))
``````

# Weight operations

Use Magic Methods to support the `+` operator in the custom built class `Weight`. Example:

``````w1 = Weight(kilograms=80)
w2 = Weight(pounds=46)
w3 = w1 + w2

print(w3.kilograms)  # 100.90
print(w3.pounds)  # 222.0
``````

You can also combine it with properties to have a more powerful solution.

### Test Cases

test add only pounds -

``````def test_add_only_pounds():
w1 = Weight(pounds=37)
w2 = Weight(pounds=22)
w3 = w1 + w2

assert w3.pounds == 59
``````

test add kilograms to pounds -

``````import pytest

def test_add_kilograms_to_pounds():
w1 = Weight(kilograms=80)
w2 = Weight(pounds=46)
w3 = w1 + w2

assert w3.kilograms == pytest.approx(100.90, rel=0.01)
assert w3.pounds == pytest.approx(222.0, rel=0.01)
``````

test add only kilograms -

``````def test_add_only_kilograms():
w1 = Weight(kilograms=80)
w2 = Weight(kilograms=25)
w3 = w1 + w2

assert w3.kilograms == 105
``````
class Weight(object): pass