-2

While I am reading the book "Using Asyncio in Python 3" I encountered a line of def definition such as below. There is "to:" keyword inside the def parenthesis and it seems like ":" here is different with "=" but I couldn't figure out what ":" is and for what purpose.

What is "to:" in below code?

@attrs
class Cutlery:
    knives = attrib(default=0)
    forks = attrib(default=0)

    def give(self, to: 'Cutlery', knives=0, forks=0):
        self.change(-knives, -forks)
        to.change(knives, forks)

    def change(self, knives, forks):
        self.knives += knives
        self.forks += forks

1 Answers1

0

to is a regular function parameter, and the colon indicates a type hint (also called annotation). This means that to is expected to be of type Cutlery. These annotations are not checked by default, but you can do so by running third-party checkers like mypy on your code.

fjarri
  • 9,546
  • 39
  • 49