solutions

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 - Run Test

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 - Run Test

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 - Run Test

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

    assert w3.kilograms == 105