2017-01-30 44 views
1

我有一個包含函數參數類型聲明如下Python腳本:的Python:函數參數類型,設置在返回的SyntaxError

def dump_var(v: Variable, name: str = None): 

據我所知,這是一個有效的語法,設置的輸入類型參數給功能,但它返回一個

SyntaxError: invalid syntax 

什麼可能是錯誤的?

+2

的Python 2.7不支持類型提示,所以這是無效的語法。 – kindall

+0

Python 2.7不支持類型提示。 – Blender

回答

2

答案很簡單:語法錯誤:無效的語法,因爲對於python2.7類型暗示是一個語法錯誤,可以選擇使用python3或使用類型提示在評論python2.7PEP建議

您可以閱讀本文,以瞭解如何在python2.7中使用類型提示Suggested syntax for python2.7 and straddling clode

Some tools may want to support type annotations in code that must be compatible with Python 2.7. For this purpose this PEP has a suggested (but not mandatory) extension where function annotations are placed in a # type: comment. Such a comment must be placed immediately following the function header (before the docstring)

一個例子:

以下Python代碼3:

def embezzle(self, account: str, funds: int = 1000000, *fake_receipts: str) -> None: 
    """Embezzle funds from account using fake receipts.""" 
    <code goes here> 

等效於以下Python代碼2.7

def embezzle(self, account, funds=1000000, *fake_receipts): 
    # type: (str, int, *str) -> None 
    """Embezzle funds from account using fake receipts.""" 
    <code goes here>