2017-08-16 18 views
0

を実行しているとき、私は私が実行すると、私はpythonの2 でそれを実行しようとしていながら、次ののpython 2構文エラーのpython 3コード

class ExperimentResult(BaseDataObject): 
    def __init__(self, result_type: str, data: dict, references: list): 
     super().__init__() 
     self.type = result_type 
     self.references = references 
     self.data = data 

    def __repr__(self): 
     return str(self.__dict__) 

のように見えますクラスは、コードはPython 3で書かれていそれは私が得る

def __init__(self, result_type: str, data: dict, references: list): 
           ^
SyntaxError: invalid syntax 

これを解決するための "import_from_future"はありますか?

+2

真ゾル** Python 2でコードを実行することを止める* –

答えて

7

いいえ、Python 2でPython 3アノテーションを有効にするスイッチはありません。タイプヒントにアノテーションを使用している場合は、代わりにコメントを使用してください。

PEP 484のSuggested syntax for Python 2.7 and straddling code部と構文の詳細についてType checking Python 2 code section参照:機能アノテーション構文が導入されて以来のPython 2.7互換性、関数型の注釈を、コメントに記載されていることが必要であるコードについて

をあなたの具体的な例はPython 3

で、それはなるだろう:

class ExperimentResult(BaseDataObject): 
    def __init__(self, result_type, data, references): 
     # type: (str, dict, list) -> None 
     super().__init__() 
     self.type = result_type 
     self.references = references 
     self.data = data