Help - Function pointers in Python

My program idea:

I have a dictionary:

options = { 'string' : select_fun(function pointer),
'float' : select_fun(function pointer),
'double' : select_fun(function pointer)
}

whatever type appears, one function select_fun(function pointer)is called. Inside select_fun(function pointer)I will have diff functions for float, double, etc.

Depending on the function pointers, the called function will be called.

I don’t know if my programming knowledge is good or bad, but I need help.

+3
source share
5 answers

Could you clarify what you are trying to do? You don't have to do anything special to get function pointers in Python - you can pass 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
+20
source

type() .

, , - , :

if type(this_is_string) == type('some random string'):
    # this_is_string is indeed a string

, :

options = { 'some string'     : string_function,
            (float)(123.456)  : float_function,
            (int)(123)        : int_function
          }

def call_option(arg):

    # loop through the dictionary
    for (k, v) in options.iteritems():

        # if found matching type...
        if type(k) == type(arg):

            # call the matching function
            func = option[k]
            func(arg)

:

call_option('123')       # string_function gets called
call_option(123.456)     # float_function gets called
call_option(123)         # int_function gets called

python , Python, , .


: @Adam , , :

from types import *

options = { types.StringType  : string_function,
            types.FloatType   : float_function,
            types.IntType     : int_function,
            types.LongType    : long_function
          }

def call_option(arg):
    for (k, v) in options.iteritems():

        # check if arg is of type k
        if type(arg) == k:

            # call the matching function
            func  = options[k]
            func(arg)

type(), :

def call_option(arg):
    func = options[type(arg)]
    func(arg)

:-) .


: ctypes, , , ctypes. [type_name_here] . , , ctypes.c_xxx.

options = { ctypes.c_long     : c_long_processor,
            ctypes.c_ulong    : c_unsigned_long_processor,
            types.StringType  : python_string_procssor
          }

call_option = lambda x: options[type(x)](x)
+6

Python, , , .

Python . Python float C double.

def process(anobject):
    if isinstance(anobject, basestring):
       # anobject is a string
       fun = process_string
    elif isinstance(anobject, (float, int, long, complex)):
       # anobject is a number
       fun = process_number
    else:
       raise TypeError("expected string or number but received: '%s'" % (
           type(anobject),))
    return fun(anobject)

functools.singledispatch, :

from functools import singledispatch
from numbers import Number

@singledispatch
def process(anobject): # default implementation
    raise TypeError("'%s' type is not supported" % type(anobject))

@process.register(str)
def _(anobject):
   # handle strings here
   return process_string(anobject)

process.register(Number)(process_number) # use existing function for numbers

Python 2 pkgutil.simplegeneric().

:

+4

, , - C, Python.

, , Python , , .

, , ; .

class StringSomething(object):
  data = None
  def data_function(self):
     string_function_pointer(self.data)

class FloatSomething(object):
  data = None
  def data_function(self):
     float_function_pointer(self.data)

.

, , python; , : -)

+4

Perhaps you want to call the same select_fun()every time with a different argument. If this is what you mean, you need another dictionary:

>>> options = {'string' : str, 'float' : float, 'double' : float }
>>> options
{'double': <type 'float'>, 'float': <type 'float'>, 'string': <type 'str'>}
>>> def call_option(val, func):
...     return func(val)
... 
>>> call_option('555',options['float'])
555.0
>>> 
+3
source

All Articles