Knowledge is the Only Good
  • About

Effective Python: Introspection

programming
Python
effective python
Author

Stephen J. Mildenhall

Published

2022-07-05

Get the arguments of a function. Used in aggregate.distr.Aggregate.


import inspect

def f(a, b, c, d):

    frame = inspect.currentframe()
    spec = inspect.getargvalues(frame).locals
    for n in ['frame']:
        if n in spec:
            spec.pop(n)

    # internal variables are ignored
    e = f = g = 999

    return spec


f(1,2,3,4)
>>> {'a': 1, 'b': 2, 'c': 3, 'd': 4}

Stephen J. Mildenhall. License: CC BY-SA 2.0.

 

Website made with Quarto