React List Keys: Why Position Is Not Always Identity
A list item can move while still representing the same thing. React uses keys to identify items among their siblings. Choosing a…
A Python function with a list as its default argument can behave as though it remembers a previous call. The list is not recreated automatically for every call. The default expression is evaluated when the function is defined.
def collect(item, items=[]):
items.append(item)
return items
print(collect("A"))
print(collect("B"))
The first printed result is ['A']. The second is ['A', 'B'] because both calls use the same default list. The official Python tutorial explains this behaviour for mutable defaults such as lists and dictionaries.
def collect(item, items=None):
if items is None:
items = []
items.append(item)
return items
Now separate calls without an items argument receive separate lists. If a caller supplies a list, this version still appends to that caller-supplied list. Decide whether that mutation is part of the function’s intended contract rather than assuming the None pattern removes every side effect.
In a data or backend interview, explain the expected lifetime of the collection. Shared state can be intentional, but it should be a clear choice. This example helps you spot an accidental shared object before it becomes a confusing test failure.
Explore software, cloud and data roles that fit your skills and ambitions.
Explore opportunitiesA list item can move while still representing the same thing. React uses keys to identify items among their siblings. Choosing a…
Two Java objects can describe the same business item without being the same object in memory. Before discussing equals and hashCode in…
An async operation may no longer be needed because a user leaves a screen or a time limit is reached. In C#,…