7 Replies - 272 Views - Last Post: 23 February 2014 - 09:39 PM Rate Topic: -----

#1 pineapplePie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • 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



thanks in advance.
Is This A Good Question/Topic? 0
  • +

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

#2 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 111
  • View blog
  • Posts: 204
  • 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

Was This Post Helpful? 1
  • +
  • -

#3 pineapplePie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 111
  • View blog
  • Posts: 204
  • 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
Was This Post Helpful? 2
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,125
  • 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!
Was This Post Helpful? 0
  • +
  • -

#6 pineapplePie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • 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!
Was This Post Helpful? 0
  • +
  • -

#7 BlueMelon  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • 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


Was This Post Helpful? 1
  • +
  • -

#8 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 111
  • View blog
  • Posts: 204
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1