How to get Python variable annotations?
问题 When defining a class/module with annotated fields, how can I get annotations as like in functions? class Test: def __init__(self): self.x : int t = Test() Now I need 'int' from getattr(t,'x') 回答1: With baseline Python, there is no option to do what you want without changing the definition of Test . The minimalist change would be to annotate the attribute at class level: class Test: x: int def __init__(self): # define self.x or not, but it needn't be annotated again This is actually perfectly