infering the number of args a function takes at runtime

Discussion in 'Python' started by sniffer, Dec 8, 2008.

  1. sniffer

    sniffer Guest

    hi all,
    i am a python newbie, in a project currently doing i need to find out
    the number of arguments that a function takes at runtime.? Is this
    possible ,if so how do i do this,i ve looked through the python
    documentation but couldnt find anything.any help will be great


    TIA
     
    sniffer, Dec 8, 2008
    #1
    1. Advertising

  2. sniffer

    Chris Rebert Guest

    On Sun, Dec 7, 2008 at 8:39 PM, sniffer <> wrote:
    > hi all,
    > i am a python newbie, in a project currently doing i need to find out
    > the number of arguments that a function takes at runtime.? Is this
    > possible ,if so how do i do this,i ve looked through the python
    > documentation but couldnt find anything.any help will be great


    You want inspect.getargspec() or one of its friends in the `inspect`
    module. See http://docs.python.org/library/inspect.html#inspect.getargspec

    Cheers,
    Chris

    --
    Follow the path of the Iguana...
    http://rebertia.com
    I really should get back to studying now

    >
    >
    > TIA
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Chris Rebert, Dec 8, 2008
    #2
    1. Advertising

  3. sniffer

    James Mills Guest

    On Mon, Dec 8, 2008 at 2:45 PM, Chris Rebert <> wrote:
    > On Sun, Dec 7, 2008 at 8:39 PM, sniffer <> wrote:
    >> hi all,
    >> i am a python newbie, in a project currently doing i need to find out
    >> the number of arguments that a function takes at runtime.? Is this
    >> possible ,if so how do i do this,i ve looked through the python
    >> documentation but couldnt find anything.any help will be great

    >
    > You want inspect.getargspec() or one of its friends in the `inspect`
    > module. See http://docs.python.org/library/inspect.html#inspect.getargspec


    Also, I have to ask: Why do you need to do this ?
    Please show some code samples of what you're trying to achieve
    and what the problem is ?

    cheers
    James

    --
    --
    -- "Problems are solved by method"
     
    James Mills, Dec 8, 2008
    #3
  4. sniffer

    sniffer Guest

    On Dec 8, 9:39 am, sniffer <> wrote:
    > hi all,
    > i am a python newbie, in a project currently doing i need to find out
    > the number of arguments that a function takes at runtime.? Is this
    > possible ,if so how do i do this,i ve looked through the python
    > documentation but couldnt find anything.any help will be great
    >
    > TIA


    Thanks guys , i think this should work for me.btw as enquired i am
    working o a mvc thingie where in need to call functions in a
    controller,and pass the arguments as recieved from the end user so i
    need to make sure that the number of arguments passed from the front
    end are correct or not in the context of the function being called
     
    sniffer, Dec 8, 2008
    #4
  5. sniffer

    Lie Ryan Guest

    On Mon, 08 Dec 2008 02:40:03 -0800, sniffer wrote:

    > On Dec 8, 9:39 am, sniffer <> wrote:
    >> hi all,
    >> i am a python newbie, in a project currently doing i need to find out
    >> the number of arguments that a function takes at runtime.? Is this
    >> possible ,if so how do i do this,i ve looked through the python
    >> documentation but couldnt find anything.any help will be great
    >>
    >> TIA

    >
    > Thanks guys , i think this should work for me.btw as enquired i am
    > working o a mvc thingie where in need to call functions in a
    > controller,and pass the arguments as recieved from the end user so i
    > need to make sure that the number of arguments passed from the front end
    > are correct or not in the context of the function being called


    In python, usually you'll just pass the them all and maybe catch errors.

    def callback(*args, **kargs):
    try:
    return func(*args, **kargs)
    except TypeError:
    print('An error happened')
     
    Lie Ryan, Dec 8, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    507
    David Eppstein
    Sep 17, 2003
  2. Replies:
    4
    Views:
    578
  3. ashu
    Replies:
    1
    Views:
    478
  4. ashu
    Replies:
    2
    Views:
    626
    mysticlol
    Nov 6, 2006
  5. Olaf
    Replies:
    6
    Views:
    1,044
    Paul Uiterlinden
    May 29, 2007
Loading...

Share This Page