Standard Classes

Introduction

The standard subpackage provides base classes, decorators, and class factories (metaclasses) to imbue classes, and the instances which they produce, with attributes concealment and immutability.

>>> import classcore.standard as ccstd

Inheriting from a standard base class:

>>> class Point2d( ccstd.Object ):
...     def __init__( self, x: float, y: float ) -> None:
...         self.x = x
...         self.y = y
...
>>> point = Point2d( 3, 4 )
>>> type( Point2d )
<class 'classcore.standard.classes.Class'>

is essentially equivalent to producing a new class with a standard metaclass:

>>> class Point2d( metaclass = ccstd.Class ):
...     def __init__( self, x: float, y: float ) -> None:
...         self.x = x
...         self.y = y
...
>>> point = Point2d( 5, 12 )

Concealment and Immutability

Both classes have immutable attributes. For example, we cannot delete the __init__ method that we defined:

>>> del Point2d.__init__
Traceback (most recent call last):
...
classcore.exceptions.AttributeImmutability: Could not assign or delete attribute '__init__' on class ...

Nor, for example, can we add a default value for x:

>>> Point2d.x = 3
Traceback (most recent call last):
...
classcore.exceptions.AttributeImmutability: Could not assign or delete attribute 'x' on class ...

Also, all non-public attributes on the class are concealed from dir():

>>> dir( Point2d )
[]

The instances of these classes also have immutable attributes:

>>> point.x = 3
Traceback (most recent call last):
...
classcore.exceptions.AttributeImmutability: Could not assign or delete attribute 'x' on instance of class ...

And concealed non-public attributes:

>>> dir( point )
['x', 'y']

Decoration versus Production

By contrast, if we decorate an existing class, then it retains the default Python behavior (full mutability and visibility) with respect to its class attributes:

>>> @ccstd.with_standard_behaviors( )
... class Point2d:
...     def __init__( self, x: float, y: float ) -> None:
...         self.x = x
...         self.y = y
...
>>> point = Point2d( 8, 15 )
>>> type( Point2d )
<class 'type'>
>>> '__init__' in dir( Point2d )
True
>>> del Point2d.__init__

However, attributes on its instances are immutable and concealed, which is the same behavior as for the classes we produced:

>>> dir( point )
['x', 'y']
>>> point.x = 5
Traceback (most recent call last):
...
classcore.exceptions.AttributeImmutability: Could not assign or delete attribute 'x' on instance of class ...

Thus, if you do not desire class attributes concealment and immutability, you can choose to decorate classes rather than produce them.

Mutable Instances

To produce classes with immutable attributes but instances with mutable attributes, there is a convenience class, ObjectMutable.

>>> class Point2d( ccstd.ObjectMutable ):
...     def __init__( self, x: float, y: float ) -> None:
...         self.x = x
...         self.y = y
...
>>> point = Point2d( 7, 24 )
>>> point.x, point.y = 20, 21
>>> point.x, point.y
(20, 21)