class Person:
def __init__(self, ......
def make_friend(self, friend: 'Person') -> None:
.....
I'm learning python and I don't understand this example. Why type condition for friend should be in single quotes? Why not friend: Person
? How it can works? Does it have something common with type class shortcut?
The function make_friend
is defined before the class Person
.
When classes are made, first the namespace (internal class functions) is created, and then it is built into the class (using the __prepare__
method).
Trying to reference Person
before it is created will fail.
Solving it can be done with a "forward reference", which can be used with quotes or with a from __future__ import annotations
at the top of the file.