Variable Annotations On A Class
I'm trying to build up an object graph in some code where I'm using type hint on class attributes in Python 3.6. Generally this look like: class MyObject: some_variable: float
Solution 1:
This can be done using forward references. So your code would look like this:
class MyObject:
parent: 'MyObject' = None
Solution 2:
This seem to be hacky and is probably not the desired way to do this, but you can define MyObject with
class MyObject:
pass
and redefine MyObject as:
class MyObject:
parent: MyObject = None
Post a Comment for "Variable Annotations On A Class"