# How to get the number of arguments of a given function?

Page 1 of 1

## 7 Replies - 982 Views - Last Post: 23 February 2014 - 09:39 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=340614&amp;s=c643b13f595033bc5b9d70b09fd0bf51&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 pineapplePie

Reputation: 0
• Posts: 69
• Joined: 23-January 12

# How to get the number of arguments of a given function?

Posted 22 February 2014 - 01:49 AM

Hi all,

As per the tile, I want get the number of arguments of a given function.

This is what I've done so far.
```import inspect
def getFunArgNum(function):
print inspect.getargspec(function_a)

def function_a(a, b, c):
print a+b+c

getFunArgNum(function_a(2,1,3)) # expecting 3

```

Is This A Good Question/Topic? 0

## Replies To: How to get the number of arguments of a given function?

### #2 Mekire

Reputation: 118
• Posts: 216
• Joined: 11-January 13

## Re: How to get the number of arguments of a given function?

Posted 22 February 2014 - 02:13 AM

If you aren't considering functions that way take *args and **kwargs it is given by:

```def get_argument_number(function):
inspected = inspect.getargspec(function)
return len(inspected[0])
```

Spoiler

-Mek

This post has been edited by Mekire: 22 February 2014 - 02:14 AM

### #3 pineapplePie

Reputation: 0
• Posts: 69
• Joined: 23-January 12

## Re: How to get the number of arguments of a given function?

Posted 22 February 2014 - 06:54 AM

hi,
I've got following error message:
Traceback (most recent call last):
File "/home/aki/Python/test.py", line 10, in <module>
print getFunArgNum(function_a(1,2,3))
File "/home/aki/Python/test.py", line 3, in getFunArgNum
inspected = inspect.getargspec(function)
File "/usr/lib/python2.7/inspect.py", line 815, in getargspec
raise TypeError('{!r} is not a Python function'.format(func))
TypeError: 6 is not a Python function

This post has been edited by andrewsw: 22 February 2014 - 09:34 AM
Reason for edit:: Removed previous quote

### #4 Mekire

Reputation: 118
• Posts: 216
• Joined: 11-January 13

## Re: How to get the number of arguments of a given function?

Posted 22 February 2014 - 07:17 AM

Note from my example in the spoiler tags I pass the actual function object to our argument-counting-function. I don't actually call the function.

IE this:
```print getFunArgNum(function_a)
```

Not this:
```print getFunArgNum(function_a(1,2,3))
```

-Mek

### #5 jon.kiparsky

• Beginner

Reputation: 11041
• Posts: 18,857
• Joined: 19-March 11

## Re: How to get the number of arguments of a given function?

Posted 22 February 2014 - 07:26 AM

Nice one, Mek!

### #6 pineapplePie

Reputation: 0
• Posts: 69
• Joined: 23-January 12

## Re: How to get the number of arguments of a given function?

Posted 23 February 2014 - 01:25 AM

Now it is working as expected. Thank you very much!

### #7 BlueMelon

Reputation: 40
• Posts: 187
• Joined: 27-April 10

## Re: How to get the number of arguments of a given function?

Posted 23 February 2014 - 07:49 PM

You can also use this:
```>>> def function_a(a, b, c):
...     print a+b+c
...
>>> function_a.func_code.co_argcount
3

```

### #8 Mekire

Reputation: 118
• Posts: 216
• Joined: 11-January 13

## Re: How to get the number of arguments of a given function?

Posted 23 February 2014 - 09:39 PM

Interesting. I wasn't aware of func_code.

-Mek