Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to find all possible kwargs for a function in python from cli?

Is there a way to discover the potential keyword arguments for a function in python from the command line? without looking at the source or docs. Sometimes the source is to c lib even that isn't visible

like image 255
Riz Avatar asked Apr 22 '16 20:04

Riz


1 Answers

You can use the inspect module. In 3.3+, this is easy by using inspect.signature

import inspect

def foo(bar=None, baz=None):
    pass

>>> print(inspect.signature(foo))
(bar=None, baz=None)

Immediately underneath the linked doc is an example that pulls out only the names of the keyword-only arguments, which may be worth reading too!

Of course if you're looking to deep inspect the source code to try and find anything that is pulled out of a **kwargs argument, you're probably out of luck. Something like:

def foo(**kwargs):
    if kwargs.get("isawesome"):
        print("Dang you're awesome")

>>> some_magic(foo)
isawesome

is probably going to be hard to find.

like image 89
Adam Smith Avatar answered Oct 08 '22 01:10

Adam Smith