Question
Is there a way to get the argument names a function takes? For example:
- def foo(bar, buz):
- pass
- magical_way(foo) == ["bar", "buz"]
Use the inspect method from Python's standard library (the cleanest, most solid way to perform introspection).
inspect.getargspec(f) returns the names and default values of f's arguments:
A testing example:
If you only want the names and don't care about special forms *a, **k,
- import inspect
- ef magical_way(f):
- return inspect.getargspec(f)[0]
Supplement
* Getting method parameter names in python 5 answers
* Getting list of parameter names inside python function 4 answers
* Python *args and **kwargs?
沒有留言:
張貼留言