~/ learn/ comp-456/ cards/ Semantic networks & inheritance
1 of 3

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/c8dd5aa7-e75b-4340-94ab-fa9d3e6eb4e1/flashcard utf-8 LF