› 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
- This topic is empty.
-
AuthorPosts
-
August 3, 2025 at 9:28 am #5207
Node class and left child value
byu/DigitalSplendid inlearnpythonComment
byu/DigitalSplendid from discussion
inlearnpythonComment
byu/DigitalSplendid from discussion
inlearnpythonGreat question!
You’re asking: If the
Nodeclass’s constructor has a default argument likeleft_child=None, are we locking its value toNone? And then, how can it later be checked with something likeisinstance(left_child, Node)?Let’s break it down.
1. Default Argument Doesn’t Lock the Value
The default argument
left_child=Nonemeans: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:
- For
a, no left or right child is passed → both default toNone. - For
b,left_child=ais passed →left_childis now aNodeobject.
2. Using
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 objectThis works because:
- If
left_childwas not passed, its value isNone→isinstance(None, Node)returnsFalse. - If it was passed as a
Nodeobject →isinstance(left_child, Node)returnsTrue.
So no conflict exists.
Summary
left_child=Nonesets a default, not a permanent lock.- If a
Nodeobject is passed, that value replaces the default. - You can safely check if it’s a
Nodeusingisinstance().
Let me know if you want this visually illustrated or explained using your Canva design.
- For
-
AuthorPosts
- You must be logged in to reply to this topic.


