~/ learn/ comp-456/ cards/ Knowledge Representation & Expert Systems
1 of 22

Walk an is-a hierarchy to look up an inherited property, honouring a local exception (canary flies, penguin does not).

Walk an is-a hierarchy to look up an inherited property, honouring a local exception (canary flies, penguin does not).

Answer

isa = {"canary": "bird", "penguin": "bird", "bird": "animal", "ostrich": "bird"} props = { "animal": {"breathes": True}, "bird": {"flies": True, "feathers": True}, "penguin": {"flies": False}, # local override } def get_prop(node, prop): cur = node while cur is not None: if prop in props.get(cur, {}): return cur, props[cur][prop] cur = isa.get(cur) return None, None src, val = get_prop("canary", "flies") print(f"canary.flies = {val} (inherited from {src})") src, val = get_prop("penguin", "flies") print(f"penguin.flies = {val} (local override)")

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/fbdd936d-d20f-4dca-b2ab-fed9af924091/flashcard utf-8 LF