{"id":8802,"library":"zope-location","title":"Zope Location","description":"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.","status":"active","version":"6.0","language":"en","source_language":"en","source_url":"http://github.com/zopefoundation/zope.location/","tags":["zope","location","traversal","hierarchical","interface","mixin"],"install":[{"cmd":"pip install zope.location","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Provides the ILocation interface and adapter machinery used throughout the Zope ecosystem, which zope.location builds upon.","package":"zope.interface","optional":false}],"imports":[{"note":"The primary implementation class for located objects, providing `__parent__` and `__name__` attributes.","symbol":"Location","correct":"from zope.location.location import Location"},{"note":"The interface defining the contract for objects that can be located within a hierarchy (i.e., having `__parent__` and `__name__`).","symbol":"ILocation","correct":"from zope.location.interfaces import ILocation"},{"note":"A mixin class that provides the basic `__parent__` and `__name__` attributes, suitable for multiple inheritance with other base classes.","symbol":"Located","correct":"from zope.location.location import Located"}],"quickstart":{"code":"from zope.location.location import Location\nfrom zope.location.interfaces import ILocation\n\n# Define a simple located object by subclassing Location\nclass MyLocatedObject(Location):\n    def __init__(self, name=None, parent=None, value=None):\n        # Explicitly set __name__ and __parent__ during initialization\n        self.__name__ = name\n        self.__parent__ = parent\n        self.value = value\n\n# Create a hierarchy of located objects\nroot = MyLocatedObject(name='', parent=None, value=\"I am the root\")\nfolder = MyLocatedObject(name='folder', parent=root, value=\"I am a folder object\")\nitem = MyLocatedObject(name='item', parent=folder, value=\"I am an item inside the folder\")\n\n# Verify that the ILocation interface is provided by these objects\nassert ILocation.providedBy(root)\nassert ILocation.providedBy(folder)\nassert ILocation.providedBy(item)\n\nprint(f\"Root: {root.value} (name='{root.__name__}', parent={root.__parent__})\")\nprint(f\"Folder: {folder.value} (name='{folder.__name__}', parent={folder.__parent__.value})\")\nprint(f\"Item: {item.value} (name='{item.__name__}', parent={item.__parent__.value})\")\n\n# Demonstrate traversal by accessing parent attributes\nprint(f\"\\nAccessing parent from item: {item.__parent__.value}\")\nprint(f\"Accessing parent's parent from item: {item.__parent__.__parent__.value}\")","lang":"python","description":"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."},"warnings":[{"fix":"Always ensure you explicitly set `obj.__parent__ = parent_obj` and `obj.__name__ = 'obj_name'` after creating a `Location` instance, or handle their initialization within your custom class's `__init__` method.","message":"The `__parent__` and `__name__` attributes of `Location` objects are not automatically populated or managed upon instantiation. They are merely attributes that `zope.location` expects to exist.","severity":"gotcha","affected_versions":"All versions"},{"fix":"If you need a located object to also act as a container for other located objects (e.g., a folder), you must implement the container methods (like `__getitem__`, `__setitem__`, `__delitem__`) yourself, typically in a subclass or by combining `Location` with another base class.","message":"`zope.location.location.Location` objects are designed for 'locating' themselves within a hierarchy but do not inherently provide container functionality (e.g., dictionary-like `__getitem__` or `__setitem__`).","severity":"gotcha","affected_versions":"All versions"},{"fix":"Before upgrading to or installing `zope.location==6.0`, ensure your project's Python environment is running Python 3.9 or a newer compatible version.","message":"Version 6.0 of `zope.location` dropped support for Python 3.8. It now explicitly requires Python 3.9 or newer to run.","severity":"breaking","affected_versions":"6.0 and later"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure that you explicitly set `obj.__parent__ = parent_obj` and `obj.__name__ = 'child_name'` after creating your located object, or within its `__init__` method.","cause":"You are attempting to access `obj.__parent__` or `obj.__name__` on a `zope.location.location.Location` instance, but these attributes have not yet been set or initialized.","error":"AttributeError: '__parent__'"},{"fix":"If you require your located objects to also function as containers, you must implement the `__getitem__` (and potentially `__setitem__`, `__delitem__`) methods in your custom located class. `zope.location` itself focuses only on the location aspects.","cause":"You are trying to retrieve a child object from a `zope.location.location.Location` instance using dictionary-like indexing (e.g., `parent_obj['child_name']`). `Location` objects do not implement containment methods by default.","error":"TypeError: 'Location' object is not subscriptable"}]}