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

标签 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_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 参数)和复制的信息 - 使用 data_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 并在返回之前将返回信息保存到一个变量,但我真的认为这是一个更优雅的解决方案 - 我错了吗?

最佳答案

set s 是无序的(或者更好的是,它们的顺序是任意的)。除了改用有序数据类型外,您无能为力。

例如,通过删除 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

现在这段代码返回一个 tuple相反(第一个元素是“错误消息字典”的查找结果,第二个元素包含在 data_string.value 中)。

关于python - 相同的函数在 Python 中以相反的顺序给出不同的结果。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11781939/

相关文章:

python - 按索引对 pandas 数据框进行排序,然后按字母顺序排序

Python 字典键值到 Pyspark 中的数据框 where 子句

java - 为什么我的三元 boolean 运算符显示相反的结果

javascript - 浏览器之间不一致的 window.onerror 返回 true/false - 任何可用的正确返回值列表?

java - 将文件的文本作为字符串返回?

Python 和 Spark : Dataframe Write does not persist on filesystem

python - 将文件和对象写入 amazon s3

c++ - 寻找一个函数(或宏)来返回一个 boost::scoped_lock

python - 多余的 python 参数

c++ - vector 数组的均值和众数 - 如何对函数进行较小的改进