0

As I am developer of Python and works at different different technologies.

So sometimes I really feel that there should be a method which can tell both the KEYWORD ARGUMENT and POSITIONAL ARGUMENTSof any method.

Example:

response(url="http://localhost:8080/core",data={"a":"12"},status=500)

Response have many keyword/positional arguments like url,data, status.

There can be many more keyword arguments of response method which I do not mentioned in above example. So I want to know all total number of keyword argument of a method.

So if anyone knows any method in Python which can tell about keyword argument please share it.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Bharti Rawat
  • 1,949
  • 21
  • 32
  • You can actually specify the value for positional arguments also as keyword arguments. Do you want them as well? – thefourtheye Mar 16 '16 at 10:15
  • 1
    There is an answer here that covers this sort of thing: http://stackoverflow.com/questions/196960/can-you-list-the-keyword-arguments-a-python-function-receives – srowland Mar 16 '16 at 10:15
  • i Just want to know total number of keyword arguments of a mehtod. So that i can use them as per my requirement. – Bharti Rawat Mar 16 '16 at 10:17
  • 1
    Hmmm, probably you can explain the actual problem. There might be a better/easy solution to it. – thefourtheye Mar 16 '16 at 10:18
  • [inspect.Signature](https://docs.python.org/3/library/inspect.html#inspect.signature)? It still won't help you if signature is defined as `callable(*args, **kwargs)`, which is perfectly fine in Python. – Łukasz Rogalski Mar 16 '16 at 10:50
  • Inspect tells the received arguments of a method not tell the default arguments of a method. – Bharti Rawat Mar 16 '16 at 10:57

1 Answers1

1

Try inspect module:

In [1]: def a(x, y , z): pass
In [2]: import inspect
In [3]: inspect.getargspec(a)
Out[3]: ArgSpec(args=['x', 'y', 'z'], varargs=None, keywords=None, defaults=None)

or use decorator:

def a(f):
    def new_f(*args, **kwds):
        print "I know my arguments. It:"
        print "args", args
        print "kwds", kwds
        print "and can handle it here"
        return f(*args, **kwds)
    return new_f
@a
def b(*args, **kwargs):
    pass

b(x=1, y=2)
Michael Kazarian
  • 4,376
  • 1
  • 21
  • 25
  • Thanks Michael. But, I already know about inspect module. It is helpful for receiving arguments not for default method arguments of python. – Bharti Rawat Mar 16 '16 at 11:01