- This topic has 0 replies, 1 voice, and was last updated August 3, 2025 at 9:28 am by .
Viewing 1 post (of 1 total)
Viewing 1 post (of 1 total)
- You must be logged in to reply to this topic.
› Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 6: Python › MITx 6.100L Introduction to CS and Programming Using Python › Node class and left child value
Node class and left child value
byu/DigitalSplendid inlearnpython
Comment
byu/DigitalSplendid from discussion
inlearnpython
Comment
byu/DigitalSplendid from discussion
inlearnpython
Great question!
You’re asking: If the Node class’s constructor has a default argument like left_child=None, are we locking its value to None? And then, how can it later be checked with something like isinstance(left_child, Node)?
The default argument left_child=None means:
If no value is passed during initialization, use
Noneas the default.
But if a value is passed, that value overrides the default.
Example:
class Node:
def __init__(self, value, left_child=None, right_child=None):
self.value = value
self.left = left_child
self.right = right_child
if left_child is not None:
print(f"Left child is: {left_child.value}")
Then this will work just fine:
a = Node(1)
b = Node(2, left_child=a)
In this case:
a, no left or right child is passed → both default to None.b, left_child=a is passed → left_child is now a Node object.isinstance(left_child, Node)You might see this used when validating the input:
if isinstance(left_child, Node):
# safe to assume it's a Node object
This works because:
left_child was not passed, its value is None → isinstance(None, Node) returns False.Node object → isinstance(left_child, Node) returns True.So no conflict exists.
left_child=None sets a default, not a permanent lock.Node object is passed, that value replaces the default.Node using isinstance().Let me know if you want this visually illustrated or explained using your Canva design.
