问题描述
我的计划的想法:
我有一本词典:
options = {'string':select_fun(函数指针),
'float':select_fun(函数指针),
'double':select_fun(函数指针)
}
无论什么类型的单一函数 select_fun(函数指针)
gets调用。
在内select_fun(函数指针)
,我会为float,double等等设置diff函数。
取决于函数指针,指定的函数将被调用。
我不知道我的编程知识是好是坏,仍然需要帮助。
您可以更具体地说明您要做什么吗?你不需要做任何特殊的事情来获得Python中的函数指针 - 你可以像普通对象那样传递函数:
def plus_1(x):
return x + 1
$ b $ def minus_1(x):
return x - 1
func_map = {'+' :plus_1,' - ':minus_1}
func_map ['+'](3)#returns plus_1(3)==> 4
func_map [' - '](3)#returns minus_1(3)==> 2
My idea of program:
I have a dictionary:
options = { 'string' : select_fun(function pointer),
'float' : select_fun(function pointer),
'double' : select_fun(function pointer)
}
whatever type comes single function select_fun(function pointer)
gets called.Inside select_fun(function pointer)
,I will have diff functions for float, double and so on.
Depending on function pointers, specified function will get called.
I don't know whether my programming knowledge is good or bad, still I need help.
Could you be more specific on what you're trying to do? You don't have to do anything special to get function pointers in Python -- you can pass around functions like regular objects:
def plus_1(x):
return x + 1
def minus_1(x):
return x - 1
func_map = {'+' : plus_1, '-' : minus_1}
func_map['+'](3) # returns plus_1(3) ==> 4
func_map['-'](3) # returns minus_1(3) ==> 2
这篇关于帮助 - Python中的函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!