How netherframe Extracts "vpc" from "self.vpc = Resource(...)"
The Magic Location
The extraction happens in Construct.__call__() at lines 156-176 of base.py.
The Mechanism: Object Identity Matching
Step 1: Resource Creation and Registration
When you write:
self.vpc = Resource(type="aws_vpc", cidr_block="10.0.0.0/16")
What happens internally:
1. Resource(...) creates a not-yet-bound resource object
2. The resource detects its parent (the stack template) via context
3. It registers itself in the parent's deferred-children list
4. The assignment self.vpc = ... stores a reference to this same object
Step 2: The Stack Has Two References to the Same Object
After self.vpc = Resource(nf_type="aws_vpc", cidr_block="10.0.0.0/16"):
Stack._pending_children = [<Resource object at 0x12345>]
Stack.vpc = <Resource object at 0x12345> # SAME object!
The Stack now has:
- The resource in its deferred-children list
- The same resource object as the attribute self.vpc
Step 3: The Extraction Algorithm
When you bind the Stack with stack("my-stack", scope=app), the __call__ method runs:
def __call__(self, nf_id, scope):
# ... create bound_instance ...
# Adopt and bind deferred children
for pending_child in self._pending_children: # Loop through children
# FIND THE ATTRIBUTE NAME
child_attr_name = None
for attr_name, attr_value in self.__dict__.items(): # Check all attributes
if attr_value is pending_child: # <-- OBJECT IDENTITY CHECK!
child_attr_name = attr_name # Found it! attr_name = "vpc"
break
if child_attr_name:
# Use attribute name as ID
child_id = child_attr_name # "vpc"
bound_child = pending_child(nf_id=child_id, scope=bound_instance)
setattr(bound_instance, child_attr_name, bound_child)
The Key: Python's is Operator
The magic is the line:
if attr_value is pending_child:
pass
This uses Python's is operator which checks object identity (same memory location), not equality.
Visual Example
# In Stack.__init__:
resource_obj = Resource(nf_type="aws_vpc", cidr_block="10.0.0.0/16")
# Creates object at memory address 0x12345
self.vpc = resource_obj # self.vpc points to 0x12345
# Also: resource_obj added to the deferred-children list pointing to 0x12345
# In Stack.__call__:
for pending_child in self._pending_children: # pending_child = obj at 0x12345
for attr_name, attr_value in self.__dict__.items():
# When attr_name = "vpc":
# attr_value = self.vpc = obj at 0x12345
# pending_child = obj at 0x12345
if attr_value is pending_child: # True! Same object!
child_attr_name = "vpc" # <-- EXTRACTED!
Why This Works
- Object Identity: Python objects have unique memory addresses
- The
isoperator: Checks if two references point to the same object - Assignment doesn't copy:
self.vpc = resourcemakes both names point to the same object
Edge Cases
Works ✅
self.vpc = Resource(nf_type="aws_vpc", cidr_block="10.0.0.0/16") # Attribute name "vpc" becomes ID
self.subnet = Resource(nf_type="aws_subnet", cidr_block="10.0.1.0/24") # Attribute name "subnet" becomes ID
Doesn't Work ❌
Resource(nf_type="aws_vpc", cidr_block="10.0.0.0/16") # Not assigned to any attribute - no ID to extract!
self.resources = [
Resource(nf_type="aws_subnet", cidr_block="10.0.1.0/24"),
Resource(nf_type="aws_subnet", cidr_block="10.0.2.0/24"),
] # In a list - no individual attribute names
Ambiguous 🤔
resource = Resource(nf_type="aws_vpc", cidr_block="10.0.0.0/16")
self.primary = resource
self.secondary = resource # Same object, two names - first one wins!
Conclusion
The extraction works by:
1. Maintaining a list of deferred children
2. Scanning the parent's __dict__ for attributes
3. Using object identity (is) to match which attribute points to each child
4. Using the attribute name as the child's ID
This is why self.vpc = Resource(...) automatically gets ID "vpc" - Python's object model makes it possible to discover which attribute name references each child object!