Skip to content Skip to sidebar Skip to footer

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:

classMyObject:
    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

classMyObject:
    pass

and redefine MyObject as:

classMyObject:
    parent: MyObject = None

Post a Comment for "Variable Annotations On A Class"