Creating Objects With `.__new__()`
Object Creation With
.__new__(). When writing Python classes, you typically don’t need to provide your own implementation of the
.__new__() special method. Most of the time, the base implementation from the built-in
object class is sufficient to build an empty object of your current class. However, there are a few interesting use cases for this method. For example, you can use
.__new__() to create subclasses of immutable types, such as
string. In the following sections of the course, you’ll learn how to write custom implementations of
.__new__() in your classes. To do this, you’ll code a few examples that will give you an idea of when you might need to override this method. To get started, in the next section, you’ll see how to make use of the
.__new__() special method in your code to provide custom object creators.
Become a Member to join the conversation.