pythonpython-3.xpython-2.7migrating

I got a syntax error during migration, functions Python3 to python2


def create_msg(content: bytes) -> bytes:
size = len(content)
return encode_msg_size(size) + content


def encode_msg_size(size: int) -> bytes:
return struct.pack("<I", size)

I want to migrate this two functions from python3 to python2.7 but i got everytime a syntax error. Does somebody has any idea?

Error: File "__init__.py", line 4 def create_msg(content: bytes) -> bytes: SyntaxError: invalid syntax


Solution

  • Function Annotations were introduced in python 3 link. remove annotations from your code:

    def create_msg(content):
        size = len(content)
        return encode_msg_size(size) + content
    
    def encode_msg_size(size):
        return struct.pack("<I", size)