2017-03-24 61 views
1

我對Python中分號何時工作以及何時不工作感到困惑。 在Python(3.4.3)的下面的代碼生成語法錯誤:無效的語法分號應該如何與Python全局聲明一起工作

global winSeqs ; if len(winSeqs) == 0: makeWinSeqs(boardSize) 

的(3.4.3)語言參考手冊第7表示:

> A simple statement is comprised within a single logical line. Several 
> simple statements may occur on a single line separated by semicolons. 
> The syntax for simple statements is: 
> 
> simple_stmt ::= expression_stmt 
>     | assert_stmt 
>     | assignment_stmt 
... 
>     | global_stmt 
>     | nonlocal_stmt 

我認爲這可能是一個錯誤(也許在文檔中),但肯定會很好地知道。

+1

我不認爲'if語句是一個簡單的語句。 –

回答

4

這與使用global無關。正如文檔所述,您可以將幾個簡單的語句與分號結合使用。

形式if ...: ...的節不是simple statement。這是一個compound statement。你會觀察到與下面的代碼相同的SyntaxError:

x = 1; if x == 1: print("wibble") 
+0

很簡單的答案。 –

+0

無線電,是有道理的。謝謝! – TonyM