4

I'm very unclear on what the "operator" -> does when used to create a function.

Like:

def create(self) -> None:
    raise NotImplementedError("It should be implemented specifically for your backend")

or

@property
def to(self) -> Identifier:
    return self._to

I've tried searching but I don't know what the "operator" is called or if it an operator at all, and using -> while searching doesn't really work :(

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
JoYiDer
  • 63
  • 8
  • Annotations do not "do" much of anything, really. At least not as of yet. – Rick Apr 25 '16 at 12:23
  • `->` is a syntax used for [Type Hinting](https://www.python.org/dev/peps/pep-0484/#type-definition-syntax) of return values of a function. – AKS Apr 25 '16 at 12:25

1 Answers1

6

You are looking at function annotations. They are not specific to property objects. Annotations let you attach arbitrary information to a function; the -> [expression] part gives information about the return value of the function. The syntax was added in Python 3.0.

In Python 3.5, a standard for adding type hinting was added to the language library, which uses function annotations to attach type information objects to the arguments and return value of functions.

You can always look at the Python reference documentation to search for specific syntax, the Full Grammar Specification should help you find the funcdef rule that contains the -> syntax, and a search of the documentation then points to the Function definitions section:

Parameters may have annotations of the form “: expression” following the parameter name. Any parameter may have an annotation even those of the form *identifier or **identifier. Functions may have “return” annotation of the form “-> expression” after the parameter list. These annotations can be any valid Python expression and are evaluated when the function definition is executed.

See PEP 3107 – Function annotations for the syntax proposal, and PEP 484 – Type Hints for information on how to use these when adding type hints.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343