帮助 - Python中的函数指针 [英] Help--Function Pointers in Python

查看:107
本文介绍了帮助 - Python中的函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的计划的想法:

我有一本词典:

  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中的函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆