views:

4982

answers:

5

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.

+8  A: 

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
Adam Rosenfield
Your answer is appropriate. But in the case of 3 as parameter i am passing a function pointer which calls a specific function inside select_fun.
@rejinacm.myopenid.com: you still don't use "pointers". You just use the function as an object. No pointer. Just pass the function around.
S.Lott
+4  A: 

You can use the type() built-in function to detect the type of the function.

Say, if you want to check if a certain name hold a string data, you could do this:

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

So in your case, you could do it like this:

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)

Then you can use it like this:

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

I don't have a python interpreter nearby and I don't program in Python much so there may be some errors, but you should get the idea.


EDIT: As per @Adam's suggestion, there are built-in type constants that you can check against directly, so a better approach would be:

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)

And since the key itself is comparable to the value of the type() function, you can just do this:

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

Which is more elegant :-) save for some error-checking.


EDIT: And for ctypes support, after some fiddling around, I've found that ctypes.[type_name_here] is actually implented as classes. So this method still works, you just need to use the ctypes.c_xxx type classes.

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)
chakrit
While technically correct, I'd argue that it would be better to compare against the constants types.FloatType, types.StringType, and types.IntType in the types module.
Adam Rosenfield
call_option = lambda arg: options[type(arg)](arg)
J.F. Sebastian
for an unsigned or signed type what should i do?
How are you getting an unsigned type in python?
recursive
I m callng a dll written in VB.I checks its type and then convert accordingly.In pyhton there is fundamental ctypes data types which has the above.I want to know how to move further...
+2  A: 

Maybe you want to call the same select_fun() every time, with a different argument. If that is what you mean, you need a different 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
>>>
gimel
options['float']('555')
J.F. Sebastian
Just trying to make some sense of the original question, asked in terms of call_option().
gimel
+2  A: 

Functions are the first-class objects in Python therefore you can pass them as arguments to other function as you would with any other object such as string or an integer.

There is no single-precision floating point type in Python. Python's float corresponds to C's 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 ValueError("expected string or number but received: '%s'" % type(anobject))
    return fun(anobject)
J.F. Sebastian
+3  A: 

Looking at your example, it seems to me some C procedure, directly translated to Python.

For this reason, I think there could be some design issue, because usually, in Python, you do not care about type of an object, but only about the messages you can send to it.

Of course, there are plenty of exceptions to this approach, but still in this case I would try encapsulating in some polymorphism; eg.

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)

etc.

Again, all of this under the assumption you are translating from a procedural language to python; if it is not the case, then discard my answer :-)

Roberto Liffredo