What is the difference between type and type.__new__ in python? What is the difference between type and type.__new__ in python? python python

What is the difference between type and type.__new__ in python?


In the first example you're creating a whole new class:

>>> class MetaA(type):...     def __new__(cls, name, bases, dct):...         print 'MetaA.__new__'...         return type(name, bases, dct)...     def __init__(cls, name, bases, dct):...         print 'MetaA.__init__'... >>> class A(object):...     __metaclass__ = MetaA... MetaA.__new__>>> 

while in the second case you're calling parent's __new__:

>>> class MetaA(type):...     def __new__(cls, name, bases, dct):...         print 'MetaA.__new__'...         return type.__new__(cls, name, bases, dct)...     def __init__(cls, name, bases, dct):...         print 'MetaA.__init__'... >>> class A(object):...     __metaclass__ = MetaA... MetaA.__new__MetaA.__init__>>> 


Please refer to the annotation below, hope this helpful.

class MetaCls(type):    def __new__(cls, name, bases, dict):        # return a new type named "name",this type has nothing        # to do with MetaCls,and MetaCl.__init__ won't be invoked        return type(name, bases, dict)class MetaCls(type):    def __new__(cls, name, bases, dict):        # return a new type named "name",the returned type         # is an instance of cls,and cls here is "MetaCls", so         # the next step can invoke MetaCls.__init__         return type.__new__(cls, name, bases, dict)


The first thing you need to figure out is how object.__new__() works.

Here it is from the documentation below:

object.__new__(cls[, ...])

Called to create a new instance of class cls. __new__() is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of __new__() should be the new object instance (usually an instance of cls).

Typical implementations create a new instance of the class by invoking the superclass’s __new__() method using super(currentclass, cls).__new__(cls[, ...]) with appropriate arguments and then modifying the newly-created instance as necessary before returning it.

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__() does not return an instance of cls, then the new instance’s __init__() method will not be invoked.

__new__() is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation.

So in mg.'s answer, the former doesn't call function __init__ while the latter calls function __init__ after calling __new__.