0

I was experimenting with creating function inside classes that work with instances of the class, I am trying to understand why I can't specify the parameter type of the function:

class MyClass:
    def __init__(self) -> None:
        self.ithink = True

    def iwork(self):
        if self.ithink: print("I am (and I work)")

    def f(i): i.iwork()

    def g(i: MyClass): i.iwork()

instance = MyClass()

MyClass.f(instance) #works
MyClass.g(instance) #doesn't
Barmar
  • 741,623
  • 53
  • 500
  • 612
ugo_capeto
  • 129
  • 8
  • 1
    The class name isn't defined until after the class definition is completed. But type hints are evaluated while the class is being defined. It's the same problem as if you wrote `listname = [listname]` – Barmar Nov 16 '22 at 23:46

0 Answers0