python中一个函数可以返回多个值吗

Python013

python中一个函数可以返回多个值吗,第1张

可以的。返回值下载return上,调用时用两个变量接收。

defF ( x, y ):

return x+y, x-y

a, b = F( 9, 4)

多个返回值需要用list集合来解析。。

举例参考一下:

import ctypes

# Load DLL into memory.

hllDll = ctypes.WinDLL ("c:\\PComm\\ehlapi32.dll")

# Set up prototype and parameters for the desired function call.

# HLLAPI

hllApiProto = ctypes.WINFUNCTYPE (

ctypes.c_int, # Return type.

ctypes.c_void_p, # Parameters 1 ...

ctypes.c_void_p,

ctypes.c_void_p,

ctypes.c_void_p) # ... thru 4.

hllApiParams = (1, "p1", 0), (1, "p2", 0), (1, "p3",0), (1, "p4",0),

# Actually map the call ("HLLAPI(...)") to a Python name.

hllApi = hllApiProto (("HLLAPI", hllDll), hllApiParams)

# This is how you can actually call the DLL function.

# Set up the variables and call the Python name with them.

p1 = ctypes.c_int (1)

p2 = ctypes.c_char_p (sessionVar)

p3 = ctypes.c_int (1)

p4 = ctypes.c_int (0)

hllApi (ctypes.byref (p1), p2, ctypes.byref (p3), ctypes.byref (p4))