What is `__new__` vs `__init__` in Python object creation?

Python Developer Hard

Python Developer — Hard

What is `__new__` vs `__init__` in Python object creation?

Key points

  • __new__ creates and returns the instance
  • __init__ initializes the instance
  • __new__ is called before __init__
  • __new__ is used for singletons and immutable types
  • __new__ must return an instance for __init__ to run

Ready to go further?

Related questions