ExtensionClass

6.2 · active · verified Thu Apr 16

This package provides a metaclass that allows classes implemented in extension modules (typically written in C) to be subclassed in Python. It's primarily used in legacy Python applications, notably within the Zope and Plone ecosystems, to provide features like a class initializer (`__class_init__`) and the acquisition-oriented `__of__` protocol, which predates some modern Python features. The current version is 6.2 and it is actively maintained by the Zope Foundation.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates the core `ExtensionClass.Base` and its `__of__` protocol, which is central to Zope's acquisition system. When an instance of `Item` is accessed via `container.item1`, the `__of__` method on `Item` is invoked, receiving the `container` as its `parent` argument.

from ExtensionClass import Base

class Container(Base):
    pass

class Item(Base):
    def __init__(self):
        self.visited = []

    def __of__(self, parent):
        # The __of__ method is called when an Item is accessed
        # through a parent (acquisition-like behavior).
        self.visited.append(parent)
        return self

container = Container()
item = Item()

print(f"Initial item visited list: {item.visited}")

# Accessing item through container triggers __of__
container.item1 = item
accessed_item = container.item1

print(f"After accessing through container: {item.visited}")

# Accessing again demonstrates __of__ being called multiple times
accessed_item_again = container.item1
print(f"After accessing again: {item.visited}")

view raw JSON →