2017-10-07 66 views
-3

是不是好有空嘗試除蟒finally塊,這樣的事情,終於嘗試 - EMPTY嘗試除了最後在python中有效嗎?

try: 
    #nothing here, 
except: 
    #nothing here as well, 
finally: 
    #nothing here too. 
+1

只需運行你的(無意義的)代碼。 –

+1

您的區塊不完全是空的 - 每個區域都有註釋。你的意思是完全空的,只包含註釋,只包含'pass'語句或其他內容?你爲什麼不試試每一個,看看會發生什麼? –

+0

@RoryDaulton解析器忽略註釋;他們不能作爲至少需要一個的陳述。 – chepner

回答

0

當然,你可以使用pass傳遞塊:

try: 
    pass 
except: 
    pass 
finally: 
    pass 

更多的例子:

def a(): 
    pass 

while True: 
    pass 

for i in range(10): 
    pass 
... 
+1

時間字面上在while循環中傳遞。 – VPfB