Describe the Differences Between `__new__` and `__init__` Methods

·

`__new__` creates a new instance of a class. `__init__` initializes the instance after it is created.

Example:

class MyClass:
    def __new__(cls):
        print('Creating instance')
        return super().__new__(cls)

    def __init__(self):
        print('Initializing instance')

obj = MyClass()

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *