-1

Let's imagine I have a class:

class Node:
    def __init__(self, value=None, next_item=None):
        self.value: int = value
        self.next_item: Node = next_item

Is it possible to "type hint" all __init__ params? Something like this:

class Node:
    def __init__(self: Self, value: int = None, next_item: Node = None):
        self.value: int = value
        self.next_item: Node = next_item

next_item, as we can see, can't refer to Node, because it doesn't yet exist. Are there any ways to do it?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Mikao Mi
  • 41
  • 4

1 Answers1

1

add line from __future__ import annotations in import and this will work with second option

sahasrara62
  • 10,069
  • 3
  • 29
  • 44