Zope Location

6.0 · active · verified Thu Apr 16

Zope Location is a small, focused library from the Zope Foundation providing an interface and a default implementation for objects that know their hierarchical position via `__parent__` and `__name__` attributes. It's a foundational component for traversal in many Zope-based applications. The current version is 6.0. It follows the Zope Foundation's release cadence, typically releasing new major versions to drop old Python support or introduce minor enhancements.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to create a simple hierarchy of objects using `zope.location.location.Location`. It highlights the necessity of explicitly setting the `__parent__` and `__name__` attributes, which are central to `zope.location`, and verifies that the `ILocation` interface is correctly provided by the objects.

from zope.location.location import Location
from zope.location.interfaces import ILocation

# Define a simple located object by subclassing Location
class MyLocatedObject(Location):
    def __init__(self, name=None, parent=None, value=None):
        # Explicitly set __name__ and __parent__ during initialization
        self.__name__ = name
        self.__parent__ = parent
        self.value = value

# Create a hierarchy of located objects
root = MyLocatedObject(name='', parent=None, value="I am the root")
folder = MyLocatedObject(name='folder', parent=root, value="I am a folder object")
item = MyLocatedObject(name='item', parent=folder, value="I am an item inside the folder")

# Verify that the ILocation interface is provided by these objects
assert ILocation.providedBy(root)
assert ILocation.providedBy(folder)
assert ILocation.providedBy(item)

print(f"Root: {root.value} (name='{root.__name__}', parent={root.__parent__})")
print(f"Folder: {folder.value} (name='{folder.__name__}', parent={folder.__parent__.value})")
print(f"Item: {item.value} (name='{item.__name__}', parent={item.__parent__.value})")

# Demonstrate traversal by accessing parent attributes
print(f"\nAccessing parent from item: {item.__parent__.value}")
print(f"Accessing parent's parent from item: {item.__parent__.__parent__.value}")

view raw JSON →