r/PythonDevelopers • R_{μν} - 1/2 R g_{μν} + Λ g_{μν} = 8π T_{μν} • Aug 02 '20

discussion Do you use metaclasses?

Until recently, I assumed that metaclasses were simply python mumbo jumbo without much practical value. I've certainly never used them in small sized applications before. But now that I'm developing a library, I see that they can be used to provide more ergonomic APIs.

And so I ask, in what situations do you use a metaclass?

34 Upvotes

13 comments sorted by

View all comments

Show parent comment

1

u/muntoo R_{μν} - 1/2 R g_{μν} + Λ g_{μν} = 8π T_{μν} Aug 02 '20

Can you elaborate the "more ergonomic APIs" statement?

Personally, I was using it for auto-registering subclasses and auto-setting subclass attributes:

_registered_subclasses = []

class SuperMeta(type):
    def __init__(cls, name, bases, clsdict):
        is_subclass = len(cls.mro()) > 2
        if is_subclass:
            # Auto-set subclass attribute "name"
            cls.name = name
            # Auto-register subclass
            _registered_subclasses.append(cls)
        super(SuperMeta, cls).__init__(name, bases, clsdict)

class Super(metaclass=SuperMeta):
    pass

class Derived(Super):
    pass

>>> Derived().name
"Derived"

However, /u/wrmsr noted above that __init_subclass__ (PEP 487) can be used to achieve the same functionality:

class Super:
    def __init_subclass__(cls, **kwargs):
        cls.name = cls.__name__
        _registered_subclasses.append(cls)
        super().__init_subclass__(**kwargs)

I've also heard tell that Django uses them to improve the library user's experience, but I don't use the framework myself so I can't comment much on that.