Hacker News new | ask | show | jobs
by samwillis 1375 days ago
This oft-repeated quote is always relevant on Python metaclass posts:

“[Metaclasses] are deeper magic than 99% of users should ever worry about. If you wonder whether you need them, you don’t (the people who actually need them know with certainty that they need them, and don’t need an explanation about why).”

Tim Peters, Inventor of the timsort algorithm and prolific Python contributor

https://www.oreilly.com/library/view/fluent-python/978149194...

https://en.m.wikipedia.org/wiki/Tim_Peters_(software_enginee...

I would then also concur with the other comment that if you “know” you need metaclasses, 99% of the time actually you only need __subclass_init__.

A lot of online literature about Python meta programming misses out __subclass_init__ as it was only added to Python 3.6 in 2015 via PEP 487.

https://peps.python.org/pep-0487/

2 comments

I've run across people in three different companies who decided to use them for no good reason at all.

I was only around for one of them while he did it but it seemed pretty clear that he was insecure and trying to prove that he was a more experienced programmer by using the most advanced features.

I call it "the advanced beginner metaclass trap".

Yes very much this. I've worked on two projects in the last year that used metaclasses. Neither needed them in the least and they simply added noise and complication.

In one case I think the author did believe he needed them, or was going to. He didn't, but I think he believed it was the best way to go about the inheritance he was doing.

The other case was exactly what you say. The person wanted to show how advanced their knowledge of Python was. The funny part about the second case (which included every advanced or new Python feature under the sun, including the walrus operator shoehorned in for good measure) the code simply didn't work at all. It ran but was wrong. The author was so busy demoing his advanced knowledge he forgot to make it work. And was unable to debug it in a timely fashion due to his overuse of abstractions and features. So less "advanced" me was brought in to make it work. Which I did.

I think you mean `__init_subclass__`
I do, thanks!