Skip to content

Commit 2eb49e9

Browse files
committed
adding generic type to children:list and adding age for the children.
1 parent 2dc539b commit 2eb49e9

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

Sprint5/generics.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from dataclasses import dataclass
2+
3+
@dataclass(frozen=True)
4+
class Person:
5+
name: str
6+
age : int
7+
children: list["Person"] #nside the Person class- the Person-type doesnt exist, so we add "" to person,
8+
9+
10+
11+
Muhib = Person(name="Muhib",age= 7, children=[])
12+
Muiz = Person(name="Muiz",age= 4,children=[])
13+
14+
Sara = Person(name="Sara",age= 31, children=[Muhib, Muiz])
15+
16+
def print_family_tree(person: Person) -> None:
17+
print(person.name)
18+
for child in person.children:
19+
print(f" {child.name} ({child.age})")
20+
21+
print_family_tree(Sara)

0 commit comments

Comments
 (0)