After publishing my previous article, on Python metaclasses, I got a fair amount of feedback, and there was one point in particular I feel I should elaborate on. It may seem simple at first glance, but it is nonetheless worth discussing in a little more detail: “Why do I use the __init__ method in my metaclass? Would __new__ be more pythonic?” It’s a fair question, but as I show here, I think the __init__ method is better for most cases.
Every article about Python metaclasses contains a quotation (yep, including this one) by Tim Peters: “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).”
With all due respect to Mr. Peters, I completely disagree. Why? Because, as an engineer, I hate magic. Moreover, I hate using calling something magic in lieu of giving a proper explanation. Metaclasses are regular tools, and they are very useful in some cases, and as such it is worth having a working understanding of them. Read more