How **not** to use __new__ in Python

Most example of python's __new__ you'll find on the interweb are at odds with Python's mottoes:

  • There should be one-- and preferably only one --obvious way to do it.
  • Simple is better than complex.
  • Readability counts.

With __new__ you can do all kinds of surprising things. That's the issue.

From the documentation:

__new__() is intended mainly to allow subclasses of immutable types (like int, str, or tuple)
to customize instance creation. It is also commonly overridden in custom metaclasses in order 
to customize class creation.

In short, for your code to smell like roses, please KISS and avoid __new__. Better, avoid classes altogether!

Comments