Motivation for custom keyword arguments to class statement

It’s not news that for a long time now (since PEP 3115) it has been possible to pass arbitrary custom keyword arguments to the class statement after the base classes. E.g.

class Bar(Foo, metaclass=Meta, private=True):

Ostensibly these custom keyword arguments can be used in some way for initializing the metaclass. What’s troubling me, besides being syntactic sugar, I can’t think of much use for this feature. The only exception I can think of is when implementing __prepare__. Since a __prepare__ method is called before evaluation of the class body, you might be able to use this to customize some aspect of how the class’s dict will be prepared. Though since Python 3.6 introduced compact dict, class definition order is preserved, and we don’t see __prepare__ used much anymore (does anyone have some interesting examples where it is used)?

So beyond that I don’t see much motivation for using this feature. The only example I can find in the Python docs is in the context of documenting __init_subclass__ which gives this example:

class Philosopher:
    def __init_subclass__(cls, /, default_name, **kwargs):
        cls.default_name = default_name

class AustralianPhilosopher(Philosopher, default_name="Bruce"):

But what is this doing that couldn’t be accomplished simply with a class attribute like:

class AustrialianPhilosopher(Philosopher):
    default_name = 'Bruce'

which personally I find clearer. But perhaps this is purely a subjective matter?

1 Like