Python: function parameter type-setting returning syntaxerror

后端 未结 1 2024
天涯浪人
天涯浪人 2021-01-22 09:35

I have a python script that contains a type declaration of function arguments as following:

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

To my

相关标签:
1条回答
  • 2021-01-22 10:00

    Short Answer : SyntaxError: invalid syntax ,because the for python2.7 type hint is a syntax violation ,you may either use python3.5+ or use type hints for python2.7 in comments as PEP suggests

    You may read this to know how to do type hints in 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)

    An example:

    the following Python 3 code:

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

    is equivalent to the following python 2.7 code

    def embezzle(self, account, funds=1000000, *fake_receipts):
        # type: (str, int, *str) -> None
        """Embezzle funds from account using fake receipts."""
        <code goes here>
    
    0 讨论(0)
提交回复
热议问题