Search code examples
pythonclassobjectpycharminstantiation

Python: How to understand __new__ as defined in the built-in object class?


First of all, this is not a duplication of Why is __init__ not called after __new__ SOMETIMES because this question really is about the implementation of the build-in object class.

Here is the full story:

I am learning about __new__ vs __init__ in python. Here is the example I've tried:

class A(object):

    def __new__(cls):
        print("A.__new__ called")
        return super(A, cls).__new__(cls)

    def __init__(self):
        print("A.__init__ called")

A()
print('finished')

The output is

A.__new__ called
A.__init__ called
finished

I understand that __new__ does object creation and __init__ does object initialization.

__new__ is called automatically when calling the class name. Then __init__ is called every time an instance of the class is returned by __new__, passing the returned instance to __init__ as the self parameter.

Knowing this, if we have a bad __new__ function that does not create and return an object instance, then __init__ would not be called:

class A(object):

    def __new__(cls):
        print("A.__new__ called")
        #return super(A, cls).__new__(cls)

    def __init__(self):
        print("A.__init__ called")

A()

Note that the __new__ method only prints a string. It does not return anything so __init__ has nothing to pick up. And indeed the output confirms this:

A.__new__ called
finished

"A.__init__ called" is never printed so __init__ is indeed never called.

Now if we do not define a __new__ method (this is 99% of the common use case. programmers rarely need to define this method), then the father's __new__ is called by default. e.g.

class A(object):

    def __init__(self):
        print("A.__init__ called")

A()
print('finished')

output is:

A.__init__ called
finished

In this case the __new__ of built-in object class is called.

BUT, as I looked at the how the built-in object class is defined, I see this:

class object:
    """ The most base type """
    def __delattr__(self, *args, **kwargs): # real signature unknown
        """ Implement delattr(self, name). """
        pass
    ...
    ...
    ...

     @staticmethod # known case of __new__
     def __new__(cls, *more): # known special case of object.__new__
         """ Create and return a new object.  See help(type) for accurate signature. """
         pass

NOTHING is implemented in the __new__! There is just a pass sitting inside !!??

How is this making sense?


Solution

  • Some of the Cpython implementation is written in C so PyCharm cannot navigate to the source code because it is compiled. However it can and does display stubs that emulate the signature.

    The source code for object.__new__ for 3.7.3 can be found in their Github repository: https://github.com/python/cpython/blob/v3.7.3/Objects/typeobject.c#L3671-L3732