相同的函数在Python中以相反的顺序给出不同的结果.为什么?

Joh*_*ode 2 python return function

我正在使用此代码:

def copy_part_of_space(row,column,lenght):
    #Copy String to Presentation Space (15)
    #Prerequisite Connect Presentation Space
    #Prerequisite function: connect_pcomm(presentation_space)    
    function_number = c_int(8)
    data_string = create_string_buffer(lenght*2*2) #number of unicode char *2*2
    lenght = c_int(lenght)
    ps_position = c_int(((row - 1) * 80)+ column)
    foo = hllapi(byref(function_number), data_string, byref(lenght), byref(ps_position))
    data_string.value
    return {{
        0 : 'The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.',
        1 : 'Your program is not connected to a host session.',
        4 : 'The host presentation space contents were copied. The connected host     presentation space was waiting for host response.',
        5 : 'The host presentation space was copied. The keyboard was locked.',
        9 : 'A system error was encountered.',
        'x' : 'Undocumented error found. Run in circles.',
        }.get(foo, 'x'),data_string.value}
Run Code Online (Sandbox Code Playgroud)

想法是从终端复制一些信息; 函数需要返回状态信息(使用字典和0,1,4,5,9,x参数)和复制的信息 - 使用data_string.value

要运行一些测试,我使用的代码使用上面的函数:

for a in range(15,22):
    print copy_part_of_space(a,7,8)
Run Code Online (Sandbox Code Playgroud)

这是结果:

   set(['The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.', '36343581'])
   set(['36343663', 'The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.'])
   set(['The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.', '36343708'])
   set(['36344673', 'The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.'])
   set(['36344740', 'The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.'])
   set(['The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.', '36344758'])
   set(['36344869', 'The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.'])
Run Code Online (Sandbox Code Playgroud)

如您所见,有时我会在从主机应用程序复制之前获取状态信息 - 就像第一行一样.

但有时我会获得在状态信息之前复制的信息,如第二行.

熟悉使用dict返回信息,所以我想这可能是一个问题,特别是在混合我试图返回两个变量的事实.

谁能解释为什么会这样?

我知道我可以简单地使用dict并在返回之前将返回信息保存到变量中,但我真的认为这是一个更优雅的解决方案 - 我错了吗?

Tim*_*ker 6

sets是无序的(或者更好,它们的顺序是任意的).除了使用有序数据类型之外,您无法做任何事情.

例如,通过删除set构造函数{...}:

return {
    0 : 'The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.',
    1 : 'Your program is not connected to a host session.',
    4 : 'The host presentation space contents were copied. The connected host     presentation space was waiting for host response.',
    5 : 'The host presentation space was copied. The keyboard was locked.',
    9 : 'A system error was encountered.',
    'x' : 'Undocumented error found. Run in circles.',
    }.get(foo, 'x'), data_string.value
Run Code Online (Sandbox Code Playgroud)

现在这段代码返回一个元组(第一个元素是来自"错误消息字典"的查找结果,第二个元素是包含的内容data_string.value).