0

I want to get the signature of Exception.__init__ using inspect module but using inspect module on Python types like Exception and None fails with the below error.

/usr/lib/python2.7/inspect.pyc in getargspec(func)
    814         func = func.im_func
    815     if not isfunction(func):
--> 816         raise TypeError('{!r} is not a Python function'.format(func))
    817     args, varargs, varkw = getargs(func.func_code)
    818     return ArgSpec(args, varargs, varkw, func.func_defaults)

TypeError: <method-wrapper '__init__' of NoneType object at 0x8ff8d0>
is not a Python function

I would like to know how signatures of in-built functions of types defined in Python can be obtained.

tdelaney
  • 73,364
  • 6
  • 83
  • 116
Krishna Oza
  • 1,390
  • 2
  • 25
  • 50
  • Can you include the offending code, and format all code and tracebacks in a code block? – Zizouz212 Aug 03 '18 at 04:59
  • Obtained from the documentation :) – wim Aug 03 '18 at 05:04
  • Possible duplicate of [Python inspect.getargspec with built-in function](https://stackoverflow.com/questions/11343191/python-inspect-getargspec-with-built-in-function) – Zizouz212 Aug 03 '18 at 05:09
  • @Zizouz212 not sure whether module functions and functions of in-build type should fall in same category – Krishna Oza Aug 03 '18 at 05:13
  • @wim I could not find the same in documentation. I found that the link https://docs.python.org/2/library/exceptions.html?highlight=exception#exceptions.BaseException has partial info but not exactly the signature. – Krishna Oza Aug 03 '18 at 05:15
  • `None` is not a type. – user2357112 Aug 03 '18 at 05:26
  • @user2357112 in Python `None` is a in-built type – Krishna Oza Aug 03 '18 at 05:29
  • @darth_coder: No, it is not. If you check `isinstance(None, type)`, you will get `False`. If you check `isinstance(3, None)` or `isinstance(any_object_here, None)`, you will get an error message complaining about how `None` is not a type or a tuple of types. – user2357112 Aug 03 '18 at 05:35
  • @user2357112 but `type(None)` gives output as `NoneType` – Krishna Oza Aug 03 '18 at 05:36
  • `NoneType` is a type. It is `None`'s type. That doesn't mean that `None` itself is a type. – user2357112 Aug 03 '18 at 05:42

1 Answers1

1

In Python 2.7, inspect does not work on functions or classes implemented in C, as they do not have the necessary object attributes for inspect to obtain details from.

This has been fixed in Python 3.x.

blhsing
  • 91,368
  • 6
  • 71
  • 106