-
Notifications
You must be signed in to change notification settings - Fork 0
/
component.py
31 lines (27 loc) · 1.11 KB
/
component.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Component:
def __init__(self, name, children=None):
if not children:
children = list()
for child in children:
if not isinstance(child, Component):
raise ValueError("Every added child must be a component"
" object.")
self.name = name
self.children = children
def add_child(self, child):
if not isinstance(child, Component):
raise ValueError("The added child must be a component object.")
self.children.append(child)
def add_children(self, children):
for child in children:
if not isinstance(child, Component):
raise ValueError("Every added child must be a component"
" object.")
self.children += children
def greet(self, name):
children_count = len(self.children)
print(f"Object id {id(self)} - Hi, {name}! I'm {self.name}. "
f"I have {children_count} children. "
"They will greet you, too. :)")
for child in self.children:
child.greet(name)