Difference between python __new__ and __init__

Difference between __new__ and __init__ in Python

__new__: Called when an object is created, it will return an instance of the current object

__init__: Called after the object is created, initializes some instances of the current object, no return value

1. In a class, if __new__ and __init__ exist at the same time, __new__ will be called first

>>> class Data(object):
...     def __new__(self):
...             print "new"
...     def __init__(self):
...             print "init"
...
>>> data = Data()
new

2. The __new__ method will return the constructed object, while the __init__ method will not. __init__ has no return value.

>>> class Data(object):
...     def __init__(cls):
...             cls.x = 2
...             print "init"
...             return cls
...
>>> data = Data()
init
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __init__() should return None, not 'Data'
>>> class Data(object):
...     def __new__(cls):
...             print "new"
...             cls.x = 1
...             return cls
...     def __init__(self):
...             print "init"
...
>>> data = Data()
new
>>> data.x =1
>>> data.x
1

 

If __new__() returns an instance of cls, then the new instance’s __init__() method will be invoked like __init__(self[, …]), where self is the new instance and the remaining arguments are the same as were passed to __new__().

If __new__ returns an instance of an object, __init__ is implicitly called

If __new__() does not return an instance of cls, then the new instance’s __init__() method will not be invoked.

__init__ will not be called if __new__ does not return an instance of the object

<class '__main__.B'>
>>> class A(object):
...     def __new__(Class):
...             object = super(A,Class).__new__(Class)
...             print "in New"
...             return object
...     def __init__(self):
...             print "in init"
...
>>> A()
in New
in init
<__main__.A object at 0x7fa8bc622d90>
>>> class A(object):
...     def __new__(cls):
...             print "in New"
...             return cls
...     def __init__(self):
...             print "in init"
...
>>> a = A()      
in New

 

object.__init__(self[, …])
Called when the instance is created. The arguments are those passed to the class constructor expression. If a base class has an __init__() method, the derived class’s __init__() method, if any, must explicitly call it to ensure proper initialization of the base class part of the instance; for example: BaseClass.__init__(self, [args…]). As a special constraint on constructors, no value may be returned; doing so will cause a TypeError to be raised at runtime.

Called after an instance of the object has been created. Parameters are passed to the constructor of the class. If the base class has an __init__ method, the subclass must explicitly call the base class's __init__.

There is no return value, otherwise a TypeError will be raised.

http://www.cnblogs.com/itaceo-o/p/3300289.html

This article is reproduced from the difference between __new__ and __init__ in python

Category: Python

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325659151&siteId=291194637