Special Methods for Classes

class sage.misc.classcall_metaclass.ClasscallMetaclass(*args)

Bases: sage.misc.nested_class.NestedClassMetaclass

A metaclass providing support for special methods for classes.

From the Section Special method names of the Python Reference Manual: `a class cls can implement certain operations on its instances that are invoked by special syntax (such as arithmetic operations or subscripting and slicing) by defining methods with special names’. The purpose of this metaclass is to allow for the class cls to implement analogues of those special methods for the operations on the class itself.

Currently, the following special methods are supported:

  • .__classcall__ (and .__classcall_private__) for customizing cls(...) (analogue of .__call__).
  • .__classcontains__ for customizing membership testing x in cls (analogue of .__contains__).
  • .__classget__ for customizing the binding behavior in foo.cls (analogue of .__get__).

See the documentation of __call__() and of __get__() and __contains__() for the description of the respective protocols.

Warning

for technical reasons, __classcall__, __classcall_private__, __classcontains__, and __classget__ must be defined as staticmethod()‘s, even though they receive the class itself as their first argument.

TODO: find a good name for this metaclass.

AUTHORS:

  • Nicolas M. Thiery (2009-2011) implementation of __classcall__, __classget__, __classcontains__;
  • Florent Hivert (2010-01): implementation of __classcall_private__, documentation.

Previous topic

Test for nested class Parent

Next topic

Unit testing for Sage objects

This Page