The __new__ method is called when a new object is created in Python. It's responsible for creating and returning the new instance of the class. This method is usually used when you want to customize object creation, such as for singleton patterns, caching, or managing memory.
The __new__ method is called before __init__ and is used to create the new object. Here's the typical order of events when you create a new object:
class Singleton: _instance = None def __new__(cls): if cls._instance is None: cls._instance = super(Singleton, cls).__new__(cls) return cls._instance s1 = Singleton() s2 = Singleton() print(s1 is s2) # True, both are the same instance
class CachedObject: _cache = {} def __new__(cls, value): if value in cls._cache: return cls._cache[value] obj = super().__new__(cls) cls._cache[value] = obj return obj obj1 = CachedObject("hello") obj2 = CachedObject("hello") print(obj1 is obj2) # True, the same object is reused
Memory Management:
If you want to control the memory allocation of objects (e.g., to optimize memory usage or manage large objects), __new__ can be used to customize how objects are created.
Immutable Objects:
__new__ is often used with immutable objects like tuples and strings. For instance, when you want to create a custom immutable object, you would override __new__ to ensure it’s properly created.
class MyTuple(tuple): def __new__(cls, *args): return super().__new__(cls, args) t = MyTuple(1, 2, 3) print(t) # (1, 2, 3)
The above is the detailed content of Pythons Magic Methods. For more information, please follow other related articles on the PHP Chinese website!