Skip to main content
Engineering LibreTexts

15.3: Every Class Is an Instance of a Metaclass

  • Page ID
    39663
    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    As we mentioned earlier in Section 15.1, classes whose instances are themselves classes are called metaclasses.

    Metaclasses are implicit

    Metaclasses are automatically created when you define a class. We say that they are implicit since as a programmer you never have to worry about them.

    An implicit metaclass is created for each class you create, so each metaclass has only a single instance.

    Whereas ordinary classes are named, metaclasses are anonymous. However, we can always refer to them through the class that is their instance. The class of SortedCollection, for instance, is SortedCollection class, and the class of Object is Object class:

    SortedCollection class
    >>> SortedCollection class
    
    Object class
    >>> Object class
    

    In fact metaclasses are not truly anonymous, their name is deduced from the one of their single instance.

    SortedCollection class name
    >>> 'SortedCollection class'
    

    Figure \(\PageIndex{1}\) shows how each class is an instance of its metaclass. Note that we only skip SequenceableCollection and Collection from the figures and explanation due to space constraints. Their absence does not change the overall meaning.

    The metaclasses of SortedCollection.
    Figure \(\PageIndex{1}\): The metaclasses of SortedCollection and its superclasses (elided).

    Querying Metaclasses

    The fact that classes are also objects makes it easy for us to query them by sending messages. Let’s have a look:

    OrderedCollection subclasses
    >>> {SortedCollection . ObjectFinalizerCollection .
        WeakOrderedCollection . OCLiteralList . GLMMultiValue}
    
    SortedCollection subclasses
    >>> #()
    
    SortedCollection allSuperclasses
    >>> an OrderedCollection(OrderedCollection SequenceableCollection
        Collection Object ProtoObject)
    
    SortedCollection instVarNames
    >>> #('sortBlock')
    
    SortedCollection allInstVarNames
    >>> #('array' 'firstIndex' 'lastIndex' 'sortBlock')
    
    SortedCollection selectors
    >>> #(#indexForInserting: #sort:to: #addAll: #reSort #sortBlock:
        #copyEmpty #addFirst: #insert:before: #defaultSort:to: #median
        #at:put: #add: #= #collect: #flatCollect: #sort: #join: #sortBlock)
    

    This page titled 15.3: Every Class Is an Instance of a Metaclass is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.