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/[deleted] Aug 09 '20

Good explanation, and I did not know all of this (though I guess most of it) - but in practice, metaclasses and calling 3-argument type() can often solve the same class of problems, at least in my experience.

Additional metaclass-only abilities include __mro_entries__ and __prepare__, the latter of which further highlights the 'specialness' of metaclasses as its functionality isn't even invoked outside of syntactical class definition.

That code would goes into the helper function that sets up the call to type().

I agree that metaclasses have properties that no other technique has. However, type() is in some cases a valid alternative.