How do I get the constructor signature for built-in types?
- From: Steven D'Aprano <steve+comp.lang.python@xxxxxxxxxxxxx>
- Date: 05 Jun 2012 05:19:06 GMT
The inspect.getargspec and getfullargspec functions allow you to extract
the function call signature for Python functions and methods. This allows
you to work out the constructor signature for pure-Python classes, by
calling inspect.getargspec on the __init__ or __new__ method.
.... def __init__(self, a, b, *args):import inspect
class X:
.... pass
....
ArgSpec(args=['self', 'a', 'b'], varargs='args', keywords=None,inspect.getargspec(X.__init__)
defaults=None)
So far so good. But if the __init__ method is inherited directly from a
built-in, getargspec fails:
.... passclass Y(int):
....
Traceback (most recent call last):inspect.getargspec(Y.__init__)
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.2/inspect.py", line 794, in getargspec
getfullargspec(func)
File "/usr/local/lib/python3.2/inspect.py", line 821, in getfullargspec
raise TypeError('{!r} is not a Python function'.format(func))
TypeError: <slot wrapper '__init__' of 'object' objects> is not a Python
function
How do I programmatically get the argument spec of built-in types'
__init__ or __new__ methods?
--
Steven
.
- Prev by Date: Re: file pointer array
- Next by Date: Metaclass of a metaclass
- Previous by thread: file pointer array
- Next by thread: Metaclass of a metaclass
- Index(es):