{"id":5832,"library":"zope-hookable","title":"Zope Hookable","description":"Zope Hookable is a Python package that facilitates the efficient creation of \"hookable\" objects. These are callable objects designed to be optionally replaced at runtime. This allows developers to define a default behavior for a function and then dynamically change its implementation, affecting all users of that function, even those who imported it. The current version is 8.2 and it is part of the Zope ecosystem, which generally follows semantic versioning with minor releases every 2-6 months and major releases every 2-3 years, though `zope.hookable` itself has a less frequent major release cycle.","status":"active","version":"8.2","language":"en","source_language":"en","source_url":"https://github.com/zopefoundation/zope.hookable","tags":["zope","hook","function","dynamic","replacement","runtime"],"install":[{"cmd":"pip install zope-hookable","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"hookable","correct":"from zope.hookable import hookable"}],"quickstart":{"code":"from zope.hookable import hookable\n\n# 1. Define a default implementation for a function\ndef _my_default_function(name):\n    return f\"Hello, {name} (default) via id {id(_my_default_function)}\"\n\n# 2. Make it hookable\nmy_hookable_function = hookable(_my_default_function)\n\n# 3. Use the hookable function - it calls the default implementation\nprint(f\"Initial call: {my_hookable_function('World')}\")\n\n# 4. Define a new implementation\ndef _my_custom_function(name):\n    return f\"Greetings, {name} (custom) via id {id(_my_custom_function)}\"\n\n# 5. Set the hook to the new implementation\nmy_hookable_function.sethook(_my_custom_function)\n\n# 6. The function now uses the new implementation\nprint(f\"After hook:   {my_hookable_function('User')}\")\n\n# 7. Define another implementation\ndef _my_another_function(name, greeting='Hi'):\n    return f\"{greeting}, {name} (another) via id {id(_my_another_function)}\"\n\n# 8. Set another hook, passing additional arguments if necessary for the new hook's signature\nmy_hookable_function.sethook(_my_another_function, 'Hola')\n\nprint(f\"Another hook: {my_hookable_function('Friend')}\")\n\n# 9. Reset to the original default implementation\nmy_hookable_function.reset()\nprint(f\"After reset:  {my_hookable_function('Again')}\")\n","lang":"python","description":"This example demonstrates how to define a hookable function, dynamically replace its implementation using `sethook`, and then revert to the original default implementation with `reset()`."},"warnings":[{"fix":"Upgrade to Python 3.10 or a newer compatible version (e.g., Python 3.11, 3.12, 3.13, 3.14 for `zope.hookable` 8.2).","message":"Older Python versions are no longer supported. `zope.hookable` has progressively dropped support for EOL Python versions. Version 8.2 requires Python 3.10 or newer. Ensure your environment meets the `requires_python` specification to avoid compatibility errors.","severity":"breaking","affected_versions":"< 8.2"},{"fix":"Set the environment variable: `export PURE_PYTHON=1` (or `set PURE_PYTHON=1` on Windows) before running `pip install zope-hookable` or executing your application.","message":"`zope.hookable` typically includes a C extension for performance. In environments where C extensions are problematic or for debugging purposes, you can force the use of a pure-Python implementation by setting the `PURE_PYTHON` environment variable to a truthy value before installing or running.","severity":"gotcha","affected_versions":"All versions with C extensions"},{"fix":"Thoroughly test changes made via `sethook` in all relevant execution paths. Consider using context managers or dependency injection patterns for more controlled dynamic behavior where global state changes might be risky.","message":"Using `zope.hookable` involves modifying global state, as changing a hook's implementation affects all parts of your application that reference that hookable object. This can lead to unexpected side effects or difficult-to-trace bugs if not managed carefully, especially in complex or multi-threaded applications. Ensure proper testing and understand the implications of runtime code replacement.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}