0

Sorry if the question is a bit confusing but I didn't know how to phrase it better.

This is what I would like to do.

class Land():
    upgrades_available = 10
    market_type: str = ""
    land_list: list[Land] = []

def __init__(self, position: str, price: int):
    self.position: str = position
    self.price: int = price
    self.upgrade_level: int = 0
    Land.land_list.append(self)

Unfortunately the interpreter doesn't like the following line:

land_list: list[Land] = []   # Unresolved reference 'Land'

I can understand why that's not possible because I imagine an infinite loop could be the result, but is there a way around this? I would like the interpreter to recognize that this list contains Land objects for ducktyping.

YepNope
  • 23
  • 3
  • 1
    `land_list: list["Land"] = []` – Matiiss Jul 27 '22 at 12:11
  • Does this answer your question? [How do I type hint a method with the type of the enclosing class?](https://stackoverflow.com/questions/33533148/how-do-i-type-hint-a-method-with-the-type-of-the-enclosing-class) – Matiiss Jul 27 '22 at 12:12
  • I'm using 3.9. land_list: list["Land"] = [] works for me, thank you! – YepNope Jul 27 '22 at 12:19

0 Answers0