Skip to content

Instantly share code, notes, and snippets.

@acbart
Created September 11, 2022 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acbart/7b082f04716f213f6bc8287f7f8fa145 to your computer and use it in GitHub Desktop.
Save acbart/7b082f04716f213f6bc8287f7f8fa145 to your computer and use it in GitHub Desktop.
Abusing the short-circuit of OR to allow recursive definitions
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