111

I just came across this function:

def splitComma(line: str):
    splits = Utils.COMMA_DELIMITER.split(line)
    return "{}, {}".format(splits[1], splits[2])

I am aware that you can separate parameters by , or can set a value within a parameter like a=39 but I have not seen a colon like line:str. I have checked the function definition online but could not find anything like this. What does this colon mean?

ruedi
  • 5,365
  • 15
  • 52
  • 88
  • 3
    Note: the duplicate discusses variable annotations, which were new to Python 3.6. This is about an ordinary function annotation, which was introduced in Python 3.0. – chepner Nov 07 '19 at 12:08

2 Answers2

132

It's a function annotation; function arguments and the return value can be tagged with arbitrary Python expressions. Python itself ignores the annotation (other than saving it), but third-party tools can make use of them.

In this case, it is intended as type hint: programs like mypy can analyze your code statically (that is, without running it, but only looking at the source code itself) to ensure that only str values are passed as arguments to splitComma.

A fuller annotation to also specify the return type of the function:

def splitComma(line: str) -> str:
    ...

(Note that originally, function annotations weren't assumed to have any specific semantics. This is still true, but the overwhelming assumption these days is that the annotations provide type hints.)

shellbot97
  • 198
  • 1
  • 12
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 1
    Thank you for this great answer. This is interesting especially for someone comming from a language where setting the type for in and output is coercively. – ruedi Mar 02 '19 at 21:01
  • 15
    Omg, I had this on a coding interview. I was thinking, "what the fk is that???" xD Needless to say, I didn't get the job. – NoName Nov 07 '19 at 05:30
  • "A fuller annotation to also specify the return type of the function:" you meant function annotation? – Geeocode Dec 28 '19 at 14:42
  • @Geeocode No, fuller; the OP's only provided an annotation on the argument, rather than the argument and the return type. – chepner Jan 02 '20 at 00:11
  • 1
    Thank you chepner, for first reading I thought that "fuller" is some special notion, what I don't know, but now I realized that this is the literal meaning of fuller. – Geeocode Jan 02 '20 at 00:24
  • Why does Python ignore this? It would be as simple as comparing the `type` of the input. Is there some reason in the documentation or something? Just curious:) – krenerd Aug 06 '21 at 06:18
  • Python is committed to remaining a dynamically typed language. It ignores the static type hints by choice, not because it would be difficult to enforce the type hints at run time. (This is also a simple case; being a dynamically typed language also means that there are situations where you cannot supply a static type hint that would capture the possible uses of a function.) – chepner Aug 06 '21 at 11:57
  • Is this means that, its like a comment, but written inside the code? As you said it will be ignored by Python. Can we say like that? – curiouscheese Mar 16 '22 at 03:35
  • It's a little stronger than a comment. The parser removes comments so that they aren't part of the abstract syntax tree (AST) that is used to generate the executable code (byte code, in the case of CPython). Annotations, on the other hand, are retained and accessible at runtime if the code itself wants to see them. – chepner Mar 16 '22 at 11:57
15

This is a type annotation used by static analysis tools to check, well, types. It helps ensure program correctness before you run the code.

wbadart
  • 2,583
  • 1
  • 13
  • 27