Search code examples
pythonhy

Syntax for specifying Metaclasses in Hy


What is the syntax for for specifying metaclasses in Hy. I tried the following:

(defclass Metaclass [] )

(defclass Foo [ :meta Metaclass ] )
(defclass Foo [ [:meta Metaclass] ] )

but this did not work


Solution

  • You do it the same way you would in Python.

    This is on Python 3 and the latest master of Hy, and using the --spy option in the repl to show the Python compilation.

    => (defclass Foo [:metaclass print])
    class Foo(metaclass=print):
        pass
    
    
    None
    
    Foo () {'__module__': '__console__', '__qualname__': 'Foo'}
    

    On Python 2.

    => (defclass Foo []
    ...  (setv __metaclass__ print))
    class Foo:
        __metaclass__ = print
    
    
    None
    
    Foo () {'__module__': '__console__', '__metaclass__': <built-in function print>}
    

    If you need it to work for either, you could try installing the six compatibility library.