6
def thefunction(a=1,b=2,c=3):
    pass

print allkeywordsof(thefunction) #allkeywordsof doesnt exist

which would give [a,b,c]

Is there a function like allkeywordsof?

I cant change anything inside, thefunction

Rostyslav Dzinko
  • 39,424
  • 5
  • 49
  • 62
user1513192
  • 1,123
  • 4
  • 17
  • 29
  • 2
    duplicate of http://stackoverflow.com/questions/2677185/how-read-method-signature – Xavier Combelle Aug 11 '12 at 13:23
  • possible duplicate of [Getting method parameter names in python](http://stackoverflow.com/questions/218616/getting-method-parameter-names-in-python) – Marcin Aug 11 '12 at 17:58

3 Answers3

24

I think you are looking for inspect.getargspec:

import inspect

def thefunction(a=1,b=2,c=3):
    pass

argspec = inspect.getargspec(thefunction)
print(argspec.args)

yields

['a', 'b', 'c']

If your function contains both positional and keyword arguments, then finding the names of the keyword arguments is a bit more complicated, but not too hard:

def thefunction(pos1, pos2, a=1,b=2,c=3, *args, **kwargs):
    pass

argspec = inspect.getargspec(thefunction)

print(argspec)
# ArgSpec(args=['pos1', 'pos2', 'a', 'b', 'c'], varargs='args', keywords='kwargs', defaults=(1, 2, 3))

print(argspec.args)
# ['pos1', 'pos2', 'a', 'b', 'c']

print(argspec.args[-len(argspec.defaults):])
# ['a', 'b', 'c']
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
1

Do you want something like this:

>>> def func(x,y,z,a=1,b=2,c=3):
    pass

>>> func.func_code.co_varnames[-len(func.func_defaults):]
('a', 'b', 'c')
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
1

You can do the following in order to get exactly what you are looking for.

>>> 
>>> def funct(a=1,b=2,c=3):
...     pass
... 
>>> import inspect
>>> inspect.getargspec(funct)[0]
['a', 'b', 'c']
>>> 
Alagappan Ramu
  • 2,270
  • 7
  • 27
  • 37