How do I type hint a method with the type of the enclosing class?
In Python, you can use the self
keyword to refer to the instance of the enclosing class within a method. To type hint the method with the type of the enclosing class, you can use the Type[T]
notation, where T
is the name of the class. Here's an example:
from typing import Type
class MyClass:
def my_method(self: Type[MyClass]) -> None:
pass
Watch a video course
Python - The Practical Guide
This will type hint the my_method
method to take an instance of MyClass
as its first argument, and return None
.