1
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?

Jaroslav Kubacek
  • 1,387
  • 18
  • 26

1 Answers1

1

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.

Bharel
  • 23,672
  • 5
  • 40
  • 80