{"id":9426,"library":"zope-lifecycleevent","title":"Zope Lifecycle Events","description":"Zope.lifecycleevent provides a standard set of event objects and convenience functions for describing the lifecycle of objects within a Zope application or any Python system using `zope.event`. This includes events for object creation, modification, addition, removal, and copying. The library is currently at version 6.0 and actively maintained, with new minor versions typically released every 2-6 months and major versions every 2-3 years, aligning with the broader Zope ecosystem.","status":"active","version":"6.0","language":"en","source_language":"en","source_url":"http://github.com/zopefoundation/zope.lifecycleevent","tags":["zope","event","lifecycle","framework","object-oriented"],"install":[{"cmd":"pip install zope.lifecycleevent","lang":"bash","label":"Latest stable release"}],"dependencies":[{"reason":"Provides the core event publishing and dispatching mechanism that zope.lifecycleevent utilizes.","package":"zope.event"},{"reason":"Used for defining the interfaces of the various lifecycle events.","package":"zope.interface"}],"imports":[{"symbol":"created","correct":"from zope.lifecycleevent import created"},{"symbol":"modified","correct":"from zope.lifecycleevent import modified"},{"symbol":"added","correct":"from zope.lifecycleevent import added"},{"symbol":"removed","correct":"from zope.lifecycleevent import removed"},{"symbol":"copied","correct":"from zope.lifecycleevent import copied"},{"note":"Used to describe specific attribute modifications during an `modified` event.","symbol":"Attributes","correct":"from zope.lifecycleevent import Attributes"},{"note":"For directly instantiating event objects, though convenience functions (e.g., `created()`) are generally preferred for dispatch.","symbol":"ObjectCreatedEvent","correct":"from zope.lifecycleevent import ObjectCreatedEvent"}],"quickstart":{"code":"from zope.lifecycleevent import created, modified, Attributes\n\nclass Document:\n    def __init__(self, title, content):\n        self.title = title\n        self.content = content\n\n    def update(self, new_content):\n        old_content = self.content\n        self.content = new_content\n        # Example of sending a modified event with attribute description\n        # In a real app, you'd likely define an IFile interface for 'content'\n        modified(self, Attributes(None, 'content'))\n        print(f\"Document '{self.title}' content updated and modified event sent.\")\n\n# Create an object and send a creation event\ndoc = Document(\"My First Doc\", \"Initial content.\")\ncreated(doc)\nprint(f\"Document '{doc.title}' created and event sent.\")\n\n# Modify the object and send a modification event\ndoc.update(\"New updated content.\")\n\n# Note: To actually *handle* these events, you would need to set up \n# subscribers using zope.event.notify and zope.component.provideHandler (not shown here).\n","lang":"python","description":"This quickstart demonstrates how to create an object and dispatch standard lifecycle events such as `created` and `modified` using the convenience functions provided by `zope.lifecycleevent`. It also shows how to include specific modification descriptions using `Attributes` for the `modified` event. To make these events have an effect, you would typically configure event subscribers within your application, often using `zope.component` in a Zope/Plone environment."},"warnings":[{"fix":"Ensure your build environment (e.g., `setuptools`, `zc.buildout`) is updated to support PEP 420 native namespaces. For `zc.buildout`, version 5 or higher is required. Upgrade `setuptools` to its latest version: `pip install --upgrade setuptools`.","message":"Version 6.0 of `zope.lifecycleevent` replaced `pkg_resources` namespace handling with PEP 420 native namespaces.","severity":"breaking","affected_versions":"6.0 and higher"},{"fix":"Upgrade your Python environment to 3.9 or newer to use `zope.lifecycleevent` 6.0. Always check the `requires_python` metadata for the specific version you intend to install.","message":"`zope.lifecycleevent` dropped support for older Python versions in recent major and minor releases.","severity":"breaking","affected_versions":"5.0 (dropped Python 2.7, 3.5, 3.6), 5.1 (dropped Python 3.7, 3.8), 6.0 (requires >=3.9)"},{"fix":"Design event subscribers to be independent, idempotent, and side-effect-free where possible. Do not rely on specific execution order or attempt to stop an event from propagating. Handle exceptions carefully within subscribers if robust error recovery is needed for other subscribers.","message":"Zope's event system (via `zope.event`) does not guarantee subscriber call order, nor does it allow event cancellation or return values from handlers. Exceptions in a handler will stop further processing.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Prefer `zope.lifecycleevent.created(obj)` over `from zope.event import notify; from zope.lifecycleevent import ObjectCreatedEvent; notify(ObjectCreatedEvent(obj))` for simplicity and adherence to the intended API.","message":"While you can manually instantiate event objects (e.g., `ObjectCreatedEvent`) and dispatch them via `zope.event.notify()`, the documentation strongly recommends using the high-level convenience functions (`created()`, `modified()`, etc.).","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Upgrade `setuptools` to its latest version (`pip install --upgrade setuptools`) and ensure your build tools (e.g., `zc.buildout`) are also updated to versions compatible with PEP 420 namespaces (e.g., `zc.buildout >= 5`).","cause":"This error typically occurs when `zope.lifecycleevent` (or other Zope packages in version 6.x) is installed in an environment using an outdated `setuptools` or build system (like `zc.buildout` prior to version 5) that still expects the `pkg_resources` API for namespace package declarations. Version 6.x migrated to PEP 420 native namespaces.","error":"AttributeError: module 'pkg_resources' has no attribute 'declare_namespace'"},{"fix":"Always pass the target object as the first argument to the lifecycle event functions. For example, `created(my_object)` instead of `created()`.","cause":"You are calling a `zope.lifecycleevent` convenience function (like `created`, `modified`, `added`, etc.) without providing the required `object` argument, which is the entity the event pertains to.","error":"TypeError: created() missing 1 required positional argument: 'object'"},{"fix":"You must explicitly register event subscribers. In a Zope/Plone application, this is typically done using ZCML configuration (e.g., `<subscriber for=\"some.interface zope.lifecycleevent.IObjectCreatedEvent\" handler=\".my_module.my_handler\" />`). For simpler cases or in non-Zope environments, you might use `zope.component.provideHandler` with `zope.event.subscribers`.","cause":"`zope.lifecycleevent` dispatches events, but it does not automatically register any handlers to respond to them. If no subscribers are configured to listen for these events, they will be sent but will not trigger any application logic.","error":"Events are dispatched but nothing happens / No handlers seem to be registered."}]}