简体   繁体   中英

Python: Assigning function along with parameters to a variable

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.

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 .

Try the inspect module

import inspect
inspect.getargspec(func).args

will give:

['name']

It will be the same.

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

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']

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM