{"id":10372,"library":"zope-size","title":"Zope Size","description":"Zope.size provides interfaces and a simple adapter pattern to determine the 'size' of an object in a generic way, typically for display or aggregation purposes. It is part of the Zope Foundation ecosystem and is currently at version 6.0, receiving updates as part of the broader Zope component set to maintain Python compatibility and address minor issues.","status":"active","version":"6.0","language":"en","source_language":"en","source_url":"http://github.com/zopefoundation/zope.size","tags":["zope","size","interface","adapter","component-architecture"],"install":[{"cmd":"pip install zope.size","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core dependency for defining and implementing interfaces used by zope.size.","package":"zope.interface","optional":false},{"reason":"Required for registering and looking up adapters that provide size information.","package":"zope.component","optional":false}],"imports":[{"symbol":"ISized","correct":"from zope.size.interfaces import ISized"},{"symbol":"get_size","correct":"from zope.size import get_size"}],"quickstart":{"code":"import zope.interface\nimport zope.component\nfrom zope.size.interfaces import ISized\nfrom zope.size import get_size\n\n# 1. Define an interface for our object\nclass IMyObject(zope.interface.Interface):\n    \"\"\"An object with some data.\"\"\"\n\n# 2. Define a class implementing the interface\n@zope.interface.implementer(IMyObject)\nclass MyObject:\n    def __init__(self, items):\n        self.items = items\n\n# 3. Define an adapter that provides ISized for IMyObject\n@zope.component.adapter(IMyObject)\n@zope.interface.implementer(ISized)\nclass MyObjectSizer:\n    def __init__(self, context):\n        self.context = context\n\n    @property\n    def size(self):\n        return len(self.context.items)\n\n# 4. Create an instance of our object\nmy_list_obj = MyObject([1, 2, 3, 4, 5])\nmy_dict_obj = MyObject({'a': 1, 'b': 2})\n\n# 5. Register the adapter (typically done at application startup)\nzope.component.provideAdapter(MyObjectSizer)\n\n# 6. Use get_size to retrieve the size\nlist_size = get_size(my_list_obj)\ndict_size = get_size(my_dict_obj)\n\n# Print the results\nprint(f\"Size of list object: {list_size}\")\nprint(f\"Size of dictionary object: {dict_size}\")\n\n# Example of an object without a registered adapter\nclass UnsizedObject: pass\nunsized_obj = UnsizedObject()\n\ntry:\n    get_size(unsized_obj)\nexcept zope.component.interfaces.ComponentLookupError as e:\n    print(f\"\\nError for unsized object (expected): {e}\")\n","lang":"python","description":"This quickstart demonstrates how to define an object, create an adapter that implements `zope.size.interfaces.ISized` for that object's interface, register the adapter using `zope.component.provideAdapter`, and then retrieve the object's size using the `zope.size.get_size` utility function. It highlights the crucial step of adapter registration."},"warnings":[{"fix":"Upgrade your Python environment to 3.9, 3.10, 3.11, or 3.12. If you must use Python 3.8, pin `zope.size<6.0`.","message":"Version 6.0 dropped support for Python 3.8. Ensure your project is running on Python 3.9 or newer.","severity":"breaking","affected_versions":"6.0 and later"},{"fix":"Ensure that you have called `zope.component.provideAdapter(YourSizerClass)` for every type you expect to get the size of. This is typically done once at application startup.","message":"Zope.size relies heavily on `zope.component` for adapter lookup. If no adapter is registered for a given object's type/interface to provide `ISized`, `get_size` will raise a `ComponentLookupError`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"In your adapter implementing `ISized`, ensure `size` is decorated with `@property`:\n```python\n@property\ndef size(self):\n    return ...\n```","message":"The `ISized` interface requires a `size` property (not a method). Implementing it as a method instead of a property will lead to `AttributeError` when `get_size` tries to access it, or incorrect behavior.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"You must register an adapter using `zope.component.provideAdapter(YourSizerClass)` before calling `get_size` for objects of that type. Ensure your adapter correctly implements `ISized` and is registered for the correct interface/type.","cause":"The `get_size` function could not find an adapter that implements `ISized` for the type of object you passed.","error":"zope.component.interfaces.ComponentLookupError: No registered adapter for (...) for zope.size.interfaces.ISized"},{"fix":"Ensure the `size` attribute in your adapter is defined as a property using the `@property` decorator:\n```python\n@zope.interface.implementer(ISized)\nclass YourSizer:\n    def __init__(self, context):\n        self.context = context\n\n    @property\n    def size(self):\n        return len(self.context.data)\n```","cause":"Your adapter class implementing `ISized` defines `size` as a regular method or is missing it entirely, instead of a property.","error":"AttributeError: 'YourSizerClass' object has no attribute 'size'"},{"fix":"Instead of `len(my_object)`, ensure you have an `ISized` adapter registered for `my_object`'s type/interface, and then use `get_size(my_object)`.","cause":"You are likely attempting to use `len()` directly on an object for which `zope.size` is intended, but `len()` is not defined for that object. You should be using `get_size()` with an appropriate adapter.","error":"TypeError: object of type 'YourObject' has no len()"}]}