Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
The __new__()
method in Python is a special method that is called when an object is created. It's responsible for the actual creation of the object, whereas the __init__()
method is responsible for initializing the object's attributes after it's created. By default, the __new__()
method returns an instance of the class, but you can override this method to customize the object creation process.
Here's a step-by-step tutorial on how to use the __new__()
method in Python:
__new__()
method:
Create a class with an __init__()
method to initialize some instance attributes.class Person: def __init__(self, name, age): self.name = name self.age = age
person = Person("Alice", 30) print(person.name) # Output: Alice print(person.age) # Output: 30
__new__()
method:
Create a class with an __init__()
method, some instance attributes, and a custom __new__()
method. In this example, we'll create a singleton class, which means that there can only be one instance of the class.class Singleton: _instance = None def __new__(cls, *args, **kwargs): if cls._instance is None: cls._instance = super().__new__(cls) return cls._instance def __init__(self, data): self.data = data
__new__()
method.singleton1 = Singleton("data1") singleton2 = Singleton("data2") print(singleton1) # Output: <__main__.Singleton object at 0x7f8c48527400> print(singleton2) # Output: <__main__.Singleton object at 0x7f8c48527400> print(singleton1 is singleton2) # Output: True print(singleton1.data) # Output: data1 print(singleton2.data) # Output: data1
In this tutorial, you learned how to use the __new__()
method in Python to customize the object creation process. By implementing a custom __new__()
method, you can control how instances of your class are created, which can be useful for implementing design patterns like singletons or for modifying the behavior of your class during instantiation.
How to Use __new__
in Python:
__new__
method is responsible for creating a new instance of a class before __init__
initializes it.class MyClass: def __new__(cls, *args, **kwargs): instance = super(MyClass, cls).__new__(cls) # Customization logic here return instance def __init__(self, *args, **kwargs): # Initialization logic here pass
Customizing Object Creation with __new__
in Python:
class CustomClass: def __new__(cls, *args, **kwargs): # Customization logic here return super(CustomClass, cls).__new__(cls) def __init__(self, *args, **kwargs): # Initialization logic here pass
Overriding __new__
for Class Instantiation in Python:
__new__
to control the instantiation process.class CustomClass: def __new__(cls, *args, **kwargs): instance = super(CustomClass, cls).__new__(cls) # Customization logic here return instance def __init__(self, *args, **kwargs): # Initialization logic here pass
Singleton Pattern Implementation with __new__
in Python:
class Singleton: _instance = None def __new__(cls, *args, **kwargs): if not cls._instance: cls._instance = super(Singleton, cls).__new__(cls) return cls._instance
Using super()
with __new__
in Python:
super()
to call the parent class's __new__
method.class CustomClass: def __new__(cls, *args, **kwargs): instance = super().__new__(cls) # Customization logic here return instance
Immutable Objects and the __new__
Method in Python:
__new__
to create immutable objects.class ImmutableClass: def __new__(cls, *args, **kwargs): instance = super().__new__(cls) # Customization logic here return instance
__new__
in Python:__new__
.def dynamic_class_creator(class_name, base_classes, class_attrs): return type(class_name, base_classes, class_attrs) DynamicClass = dynamic_class_creator('DynamicClass', (object,), {'attr': 'value'})