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

在Python中,相同的函数以相反的顺序给出不同的结果。为什么?,python,function,return,Python,Function,Return,我正在使用以下代码: 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_s

我正在使用以下代码:

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}
想法是从终端复制一些信息;函数需要返回状态信息(使用字典和0,1,4,5,9,x参数)和复制的信息-使用数据_string.value

为了运行一些测试,我使用了使用上述函数的代码:

for a in range(15,22):
    print copy_part_of_space(a,7,8)
结果如下:

   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.'])
如您所见,有时我会在从主机应用程序复制内容之前获取状态信息,如第一行

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

我不熟悉使用
dict
返回信息,所以我想这可能是个问题,特别是当我试图返回两个变量时

有人能解释为什么会这样吗

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

是无序的(或者更好,它们的顺序是任意的)。对此,除了使用有序数据类型之外,您无法执行任何操作

例如,通过删除
集合
构造函数
{…}

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

现在,该代码返回一个值(第一个元素是“错误消息字典”的查找结果,第二个元素包含在
数据\u string.value
中)

您具体返回的是一个
集合
,该集合被定义为无序数据类型。也就是说,集合的元素可以以任何顺序返回。集合针对成员资格测试进行了优化(
如果集合中有x:
)。集合就像字典的键:它们可以按任意顺序迭代

我想对您来说更好的数据类型应该是元组:
return(a,b)

然后结果将始终以相同的顺序排列

请注意文字符号的差异:

  • 字典有冒号来配对项。{'a':'b','c':'d')
  • 集合没有冒号,只是任意排序的项:
    {'a','b','c','d'}
  • 元组使用括号:
    ('a','b','c','d')
  • 列表使用方括号,并且是可变的:
    ['a','b','c','d']

它们的顺序是任意的
,但在你做某事时可能会改变。这种情况下通常要做的事情是返回一个元组。@MarkRansom:这就是当
构造函数被删除时发生的情况。我想你在我撰写注释时或之后用元组编辑了示例。@MarkRansom:可能是在你编写注释时nting;)忍者编辑FTW!元组通常用括号括起来,但正是逗号使其成为元组,因此
'a',b',c',d'
也是元组。括号是可选的,但空元组除外,或者出于语法原因(例如,在没有paren的函数参数列表中,逗号将分隔参数)@Duncan:没错。一元素元组也需要逗号。可以说逗号创建元组,但它们不是语法的一部分。对于一元素元组,只需要逗号,不需要括号:
x=1,
将创建元组。