0
def test(name):
    print "name:", name

func = test
func("testing") # it works, as I know that the function test accepts one parameter.

My question is, what if "test" has varying number of arguments depending on the scenario and how "func" knows how many number of arguments to pass and what are those arguments name.

Sorry, if I am not clear. This would give more clear picture on the scenario.

I have a function dispatcher.

testcase_obj  = testcase() # A object of a class    
if command.startswith("test1"):    
    output = exec_test1()    
elif command.startswith("do_test"):    
    output = exec_do_test(testcase_obj)

Now, I want to wrap a function whenever user sends an option while executing the script. I changed above code as:

testcase_obj  = testcase() # A object of a class    
if command.startswith("test1"):    
    func = exec_test1() # Mistake, this should be func = exec_test1
elif command.startswith("do_test"):    
    func = exec_do_test(testcase_obj) # I don't know how assign exec_do_test along
                                      # with its parameter to 'func'. I don't want to
                                      # to call exec_to_test.

if option_given:    
    func = wrapper_func(func)    
    output = func() # At this point I don't how many parameters that "func" takes.
2
  • 3
    You'll need to give more specifics about what you're trying to do. It doesn't make sense to try to call a "mystery function" without knowing what arguments it accepts. Even if you can programmatically figure out how many arguments it takes and what their names are, how would you decide what values to pass for them without reading the documentation and knowing what they do? Commented Oct 23, 2012 at 6:19
  • Your edited question still doesn't really explain how you hope to make use of the wrapped function. Do you know what exec_test1() and exec_do_test(testcase_obj) return? If you do, then you know what func will be in each case, and how many arguments to pass in each case. If you don't, knowing how many arguments they take won't help you, because you still won't know what values you can meaningfully pass for those arguments. Commented Oct 23, 2012 at 7:03

5 Answers 5

5

After saying func = test, func becomes just another name for test. So, you call func exactly the same way as test, and if you give func the wrong number of arguments you'll get a TypeError as if you had called test incorrectly.

For more information about the difference between variables in other languages and names in Python, see Code like a Pythonista.

Sign up to request clarification or add additional context in comments.

2 Comments

If "test" takes variable number of arguments and it is assigned to "func". I want to know how many arguments that "func" has. Introspection(dir(func)) will not show how many arguments that "func" can take.
Well, dir(test) wouldn't tell you either. Remember, they are names for the same exact thing. You probably want help(func)...
3

Try the inspect module

import inspect
inspect.getargspec(func).args

will give:

['name']

Comments

1

It will be the same.

func is just an alias to test, not a function calling test

Comments

1

If "test" takes variable number of arguments and it is assigned to "func". I want to know how many arguments that "func" has. Introspection(dir(func)) will not show how many arguments that "func" can take.

func is not a function. It is just an alias pointing to the function that is called test. So there is no way func can take a different number of arguments than test because func is not a function, just a name pointing to one. You can verify this:

>>> def test(arg1):
...    print 'test was given ',arg1
...
>>> func = test
>>> test.func_name
'test'
>>> func.func_name
'test'
>>> id(func)
3075004876L
>>> id(test)
3075004876L
>>> inspect.getargspec(func).args
['arg1']
>>> inspect.getargspec(test).args
['arg1']

2 Comments

func and test are absolutely equivalent. Both are names pointing to a function. So nothing special about func.
Thanks for the answer. Is it possible to assign 'test' along with its signature 'arg1' to 'func'? So that if I call func(), it should translate to the call test(arg1)
0

Yes there is a way if you give default values to your function.

def test(name="default",hi=0):
    print "name:", name,hi

func = test
func("testing")
func("testing",6) 
func(0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.