attribtool

Author: Jacek Kotlarski –<szumak@virthost.pl> Created: 02.07.2023

Purpose: Provide helpers that restrict adding attributes to classes and instances.

The module collects mixins and metaclasses that prevent accidental creation of new attributes at runtime. Inspired by recipes from Python Cookbook (2004, Martelli et al.).

class jsktoolbox.attribtool.NoNewAttributes[source]

Bases: object

Prevent instances of subclasses from gaining new attributes.

### Purpose: Blocks dynamic attribute creation by overriding __setattr__ for both instances and the metaclass.

class jsktoolbox.attribtool.NoDynamicAttributes[source]

Bases: object

Mix-in that disallows adding attributes to class instances.

### Purpose: Ensures all attributes must be declared up-front; runtime additions raise AttributeError.

__setattr__(name, value)[source]

Prevent dynamic attribute assignment on instances.

### Raises: * AttributeError: Attribute not previously declared.

Parameters:
Return type:

None

class jsktoolbox.attribtool.ReadOnlyClass[source]

Bases: type

Metaclass that makes class attributes immutable after definition.

### Purpose: Raises AttributeError whenever class-level attributes are reassigned.

__setattr__(name, value)[source]

Block assignment to class-level attributes.

### Raises: * AttributeError: Always raised to prevent modification.

Parameters:
Return type:

None

__init__(*args, **kwargs)
mro()

Return a type’s method resolution order.