Skip to content

Commit 4e124ef

Browse files
committed
docs: more tips and tricks for custom classes
1 parent 4e85c7a commit 4e124ef

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

docs/others/work_with_classes.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Working with Classes
2+
3+
Custom classes can be assigned to Node as `data` attribute, or any other attribute name.
4+
5+
```python
6+
from bigtree import Node
7+
8+
9+
class Folder:
10+
def __init__(self, name: str):
11+
self.name = name
12+
13+
def __str__(self):
14+
return f"Folder<{self.name}>"
15+
16+
class File:
17+
def __init__(self, name: str):
18+
self.name = name
19+
20+
def __str__(self):
21+
return f"File<{self.name}>"
22+
23+
folder_documents = Node("My Documents", data=Folder("Documents"))
24+
file_photo1 = Node("photo1.jpg", data=File("photo.jpg"))
25+
file_photo2 = Node("photo2.jpg", data=File("photo.jpg"))
26+
folder_documents.children = [file_photo1, file_photo2]
27+
28+
folder_documents.show()
29+
# My Documents
30+
# ├── photo1.jpg
31+
# └── photo2.jpg
32+
33+
folder_documents.show(alias="data")
34+
# Folder<Documents>
35+
# ├── File<photo.jpg>
36+
# └── File<photo.jpg>
37+
```

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ nav:
5959
- others/nodes.md
6060
- others/merging_trees.md
6161
- others/weighted_trees.md
62+
- others/work_with_classes.md
6263

6364
theme:
6465
name: material

0 commit comments

Comments
 (0)