Skip to content

Overview

Inspector Gadget - Extended Python Inspection.

This module extends the python built-in :any:inspect module.

Functions:

Name Description
get_signature

Return signature of func as string.

getsize

Sum Size of obj and Members.

get_signature

get_signature(func, vars_=None, exclude=None)

Return signature of func as string.

>>> def func(a, b=2):
...     return a + b
>>> get_signature(func)
'func(a, b=2)'

In case of given variables vars_ the caller equivalent is returned.

>>> get_signature(func, {'a': 4, 'b': 2})
'func(4)'
>>> get_signature(func, {'a': 4, 'b': 9})
'func(4, b=9)'

This is useful in combination with vars().

>>> def func(a, b=2):
...     print(get_signature(func, vars()))
>>> func(10, b=8)
func(10, b=8)

Positional arguments:

>>> def func(a, *b):
...     print(get_signature(func, vars()))
>>> func("a", "b0", "b1")
func('a', 'b0', 'b1')

Keyword arguments (Please note, that the kwargs are sorted by name):

>>> def func(a, **kwargs):
...     print(get_signature(func, vars()))
>>> func("a", foo=4, bar=8)
func('a', bar=8, foo=4)

Parameters can be hidden via exclude by name or index.

>>> def func(a, b, c, d=None, e=None):
...     print(get_signature(func, vars(), exclude=(0, "d", "g")))
>>> func('a', 'b1', 'c2', 'd3', 'e4')
func('b1', 'c2', e='e4')

getsize

getsize(obj, blacklist=(type, ModuleType, FunctionType))

Sum Size of obj and Members.

>>> import sys
>>> sys.getsizeof('a')  # doctest: +SKIP
50
>>> sys.getsizeof('b')  # doctest: +SKIP
50
>>> sys.getsizeof(('a', 'b'))  # doctest: +SKIP
56
>>> getsize(('a', 'b'))  # doctest: +SKIP
156
>>> def myfunc(a):
...     return a
>>> getsize(myfunc)
Traceback (most recent call last):
...
TypeError: getsize() does not take argument of type: <class 'function'>