简体   繁体   English

我可以使用参数传递的函数吗?

[英]Can I use a function passed by parameters?

I have a function, and inside this function I should execute another function of 3 posibilities. 我有一个函数,在这个函数中,我应该执行另一个有3个可能性的函数。

The simple way should be passing a parameter, doing a if-elif to exec function acording to that parameter, but I'd like to know it it's posible to pass the name of a funtion by params and use that, something like: 简单的方法应该是传递参数,对参数执行if-elif来执行该函数,但是我想知道可以通过params传递函数名称并使用它,例如:

FunctionA(Param1, Param2, FunctionNameToUse)
    ....Code....
    FunctionNameToUse(Value1, Value2)
    ....Code....
endFunctionA

Depending on how functionA is called, it will use one Function or another, I dont know if I explained well... 根据functionA的调用方式,它将使用一个Function或另一个Function,我不知道我是否解释得很好...

Thanks mates! 谢谢队友!

Functions themselves are first class objects in Python. 函数本身是Python中的一流对象。 So there's no need to pass the name: pass the actual function itself, then you can call it directly. 因此,无需传递名称:传递实际的函数本身,然后可以直接调用它。

def func1(a, b):
    pass

def func2(a, b):
    pass

def functionA(val1, val2, func):
    func(val1, val2)

# now call functionA passing in a function
functionA("foo", "bar", func1)

In python, functions are first-class values, which means they can be passed around as arguments, returned from functions, and so on. 在python中,函数是一等值,这意味着它们可以作为参数传递,从函数返回等。

So the approach you want to take should work (However you're not passing the name, you're passing the actual function). 因此,您要采用的方法应该可以工作(但是,您不传递名称,而是传递实际的函数)。

Take a look at map and filter in the standard library, they do exactly this. 看一下标准库中的mapfilter ,他们正是这样做的。

Yes, you can. 是的你可以。 As other have mentioned, functions are first-class objects, ie you can do everything you can think of to it (see What are "first class" objects? for more info). 正如其他人提到的那样,函数是一流的对象,即您可以想到的一切(有关更多信息,请参见“ 什么是一流的”对象? )。

So then, how does this really work when you pass in a function as parameter? 那么,当您将函数作为参数传入时,这实际上如何工作? If you have taken any ComSci class, you'd know that each process has a special area in memory allocated for its code; 如果您选择了ComSci类,您将知道每个进程在为其代码分配的内存中都有一个特殊的区域。 all of your functions are defined here. 您的所有功能都在这里定义。 Whenever you call a function, the CPU would move the Program Counter (a special pointer that points to whatever code it is executing at the moment) to the beginning of that function's code, and go from there. 每当调用函数时,CPU都会将程序计数器(指向当前正在执行的任何代码的特殊指针)移动到该函数代码的开头,然后从那里开始。 Thus, after the function is loaded in memory, all that's needed to call it is its address. 因此,在将函数加载到内存中之后,调用它所需要的只是它的地址。 When you pass a function A's "name" as an argument to another function B in Python, what you are doing is really passing a reference (in C term, a pointer) that contains the address to the code that represents A in memory. 当您将函数A的“名称”作为参数传递给Python中另一个函数B的参数时,您实际上是在传递引用(在C术语中为指针),该引用包含表示内存中代表A的代码的地址。

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

相关问题 如何在嵌套函数中使用函数的参数? - How can I use the parameters of a function inside a nested function? 在Python中,如何获取带有参数传递给我的装饰器的函数的名称? - In Python, how can I get the name of the function passed to my decorator with parameters? 传递给函数的打印参数 - Print parameters passed to the function 我不能在我的 function 的参数上使用 Python 模块 - I can not use Python modules on the parameters in my function 我可以使用字典来调用相同的函数但不同的参数吗? - Can I use dictionary for calling same function but different parameters? 如何使用现有变量及其值作为函数的参数(Python)? - How can I use existing variables and their values as parameters for a function (Python)? 我可以执行作为字符串传递的lambda函数 - can i execute a lambda function passed as a string 在pytest中,如何访问传递给测试的参数? - In pytest, how can I access the parameters passed to a test? 我可以将 map 参数传递给 docker 图像到 python 中的环境变量吗? - can i map parameters passed to a docker image to env variables in python? 在 Django 中,我可以使用传递给函数的变量来访问模型的属性吗? - In Django, can I use a variable passed to a function in order to access and attribute of a model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM