15
import inspect

class Foo(object):
    pass

if __name__ == '__main__':
    print type(Foo.__init__)
    print inspect.getsourcelines(Foo.__init__)

Output:

<type 'wrapper_descriptor'>
Traceback (most recent call last):
  *snip*
  File "/usr/lib/python2.7/inspect.py", line 420, in getfile
    'function, traceback, frame, or code object'.format(object))
TypeError: <slot wrapper '__init__' of 'object' objects> is not a module, class, method, function, traceback, frame, or code object

Googling gives very little useful information about what, exactly, a wrapper_descriptor is, and why an empty class has an __init__ method that is not a method, but rather a wrapper_descriptor.

What exactly is going on here? Do all classes without __init__ methods have one of these wrapper_descriptor things? Why is there an __init__ in the class dict at all?

martineau
  • 119,623
  • 25
  • 170
  • 301
elhefe
  • 3,404
  • 3
  • 31
  • 45

1 Answers1

10

What you've run into is an implementation detail. This is pretty typical for classes implemented in C, as object is. It's not a Python method, it is a C method, and the wrapper is part of this interface.

Why is there an __init__ in the class dict at all?

It's not in the class dict, it's in the object dict. object has an __init__ so that when you try to call your class's base classes' __init__ methods using super(), it doesn't fail.

kindall
  • 178,883
  • 35
  • 278
  • 309
  • The C API documentation explains some of the details beyond this, if you care. But normally you don't care beyond the fact that "slot wrapper", "method-wrapper", and similar are things that you can call as if they were bound methods, unbound methods, etc., but don't have any Python source code. – abarnert Mar 19 '13 at 23:38
  • this wrapper_description has no __annotations__ member, any way to bypass this non generalization – Veltzer Doron Jul 05 '18 at 15:25