简体   繁体   English

Python:将函数以及参数分配给变量

[英]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 . 说出func = testfunc 只是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. 因此,您以与test完全相同的方式调用func ,并且如果您给func错误数量的参数,您将得到TypeError ,就像您未正确调用test

For more information about the difference between variables in other languages and names in Python, see Code like a Pythonista . 有关其他语言中的变量与Python中的名称之间的区别的更多信息,请参见类似Pythonista的代码

Try the inspect module 尝试inspect模块

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 func只是要测试的别名,而不是调用test的函数

If "test" takes variable number of arguments and it is assigned to "func". 如果“ test”采用可变数量的参数,则将其分配给“ func”。 I want to know how many arguments that "func" has. 我想知道“ func”有多少个参数。 Introspection(dir(func)) will not show how many arguments that "func" can take. 内省(dir(func))将不会显示“ func”可以接受多少个参数。

func is not a function. func不是函数。 It is just an alias pointing to the function that is called test . 它只是指向称为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 . 因此, func不能采用与test不同数量的参数,因为func 并不是一个函数,只是一个指向一个的名称 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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