Created
September 11, 2022 02:45
-
-
Save acbart/7b082f04716f213f6bc8287f7f8fa145 to your computer and use it in GitHub Desktop.
Abusing the short-circuit of OR to allow recursive definitions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dataclasses import dataclass | |
@dataclass | |
class Empty: | |
pass | |
@dataclass | |
class Tree: | |
value: int | |
left: Empty or Tree | |
right: Empty or Tree | |
root = Tree(5, Tree(4, Tree(3, Empty(), Empty()), | |
Empty()), | |
Tree(4, Empty(), Empty())) | |
print(root) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment