Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Creating Objects With .__new__()
00:00
Object creation with __new__. When writing Python classes, you typically don’t need to provide your own implementation of the __new__ special method.
00:10
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.
00:22
For example, you can use __new__ to create subclasses of immutable types, such as int, float, tuple, and string.
00:29
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.
00:40
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.