type()
.
, , - , :
if type(this_is_string) == type('some random string'):
, :
options = { 'some string' : string_function,
(float)(123.456) : float_function,
(int)(123) : int_function
}
def call_option(arg):
for (k, v) in options.iteritems():
if type(k) == type(arg):
func = option[k]
func(arg)
:
call_option('123')
call_option(123.456)
call_option(123)
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():
if type(arg) == k:
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)