Skip to content
Tutorial

Find your first objects

Build a small collection of exportable objects, then find them through their shared parent type. By the end, your program will retrieve one object, list every match, inspect an item description and handle a missing type.

Prepare your environment

Use Python 3.11–3.15 with Lookups installed. Follow Install Lookups if you need to prepare an environment.

Provide objects and find one by its parent type

Create first_lookup.py with this code:

first_lookup.py
from lookups import NoneLookupError, fixed


class Exportable:
    def __init__(self, name: str) -> None:
        self.name = name

    def export(self) -> str:
        return f"Exporting {self.name}"


class Report(Exportable):
    pass


class Chart(Exportable):
    pass


report = Report("report.txt")
chart = Chart("chart.svg")
available = fixed(report, chart, "Preview")  # (1)!

exportable = available.lookup(Exportable)
if exportable is not None:
    print(exportable.export())
  1. fixed takes the objects themselves. Its membership stays the same for the lifetime of the lookup, although the objects can still change internally.

Run the program:

Run the example
python first_lookup.py
Expected output
Exporting report.txt

Exportable expresses a capability: an object with this parent type provides export(). Both Report and Chart inherit it, so both match the searched type. The string is also a member, but it does not match Exportable.

Here, fixed checks members in argument order, so the single-object query returns report. The is not None check handles the possibility that a query finds no object.

Inspect all matches and one item

Append this code to first_lookup.py:

first_lookup.py
matches = available.lookup_all(Exportable)
print("All exports:", sorted(member.name for member in matches))

item = available.lookup_item(Report)
if item is not None:
    print("Item type:", item.get_type().__name__)
    print("Same report:", item.get_instance() is report)

Run python first_lookup.py again. After the first message, you will see:

Expected output
All exports: ['chart.svg', 'report.txt']
Item type: Report
Same report: True

lookup_all retrieves the matching objects. Sorting their names gives the printed list a predictable order independently of the source's query order. An item describes one member: get_type() reports its type and get_instance() retrieves the object. The identity check confirms that this item supplies the report you created.

Handle a missing type

No integer was supplied to the lookup. Append these queries to see the two ways to handle its absence:

first_lookup.py
missing = available.lookup(int)
print("Missing integer:", missing)

try:
    available.lookup_expected(int)
except NoneLookupError:
    print("Required integer: no match")

Run python first_lookup.py again. The additional output is:

Expected output
Missing integer: None
Required integer: no match

Use lookup when absence is an expected outcome and check for None before using the object. When a matching object is required, lookup_expected returns it or raises NoneLookupError.

The complete output is:

Expected output
Exporting report.txt
All exports: ['chart.svg', 'report.txt']
Item type: Report
Same report: True
Missing integer: None
Required integer: no match

Continue with Observe a changing selection to work with membership that changes while a program runs. For the interfaces used here, see Queries, results and items and Fixed lookups. The lookup model develops the relationship between a searched type, its matching objects and their item descriptions.

Complete example

Show the complete first_lookup.py
first_lookup.py
from lookups import NoneLookupError, fixed


class Exportable:
    def __init__(self, name: str) -> None:
        self.name = name

    def export(self) -> str:
        return f"Exporting {self.name}"


class Report(Exportable):
    pass


class Chart(Exportable):
    pass


report = Report("report.txt")
chart = Chart("chart.svg")
available = fixed(report, chart, "Preview")

exportable = available.lookup(Exportable)
if exportable is not None:
    print(exportable.export())

matches = available.lookup_all(Exportable)
print("All exports:", sorted(member.name for member in matches))

item = available.lookup_item(Report)
if item is not None:
    print("Item type:", item.get_type().__name__)
    print("Same report:", item.get_instance() is report)

missing = available.lookup(int)
print("Missing integer:", missing)

try:
    available.lookup_expected(int)
except NoneLookupError:
    print("Required integer: no match")