class C { T getValueNow Python has type hints which are used by the program mypy, but I was having trouble finding how to do generic methods with it. The problem is that generic method examples shown require you to pass in a parameter with the generic type. But if the type is not used in the parameters but only in the return, as shown in the C# example above, then you'd be in trouble. The solution is to pass the type itself as a parameter, in the same way that the C# example above does but as a parameter instead of outside the parameter list. It is shown here but not as a generic function.() { } }
1 2 3 4 5 6 7 | from typing import TypeVar, Type T = TypeVar( 'T' ) class C: def get_value(return_type: Type [T]) - > T: pass |