我使用這個代碼:相同的函數在Python中以相反順序給出不同的結果。爲什麼?
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
並在返回之前將返回信息保存到變量中,但我真的認爲這是一個更優雅的解決方案 - 我錯了嗎?
'他們的順序是arbitrary'但當你做事時可能會改變 – katrielalex 2012-08-02 16:37:38
在這種情況下通常要做的事情是返回一個元組。 – 2012-08-02 16:37:56
@MarkRansom:當'set'構造函數被移除時,這裏發生了什麼。 – 2012-08-02 16:38:38