Explanation:
1. Function Definition
def make_filter(limit, seen=[]):
What happens:
Defines a function make_filter.
Takes:
limit → a threshold value.
seen → a mutable default list shared across calls.
Important: seen is created once when the function is defined, not each time it is called.
2. Returning a Lambda Function
return lambda x: x > limit and not seen.append(x)
This returns a function that:
Checks if x > limit
Appends x to seen
Uses not to invert the return value of seen.append(x)
Since:
seen.append(x) → returns None
not None → True
So the lambda returns:
True if x > limit
False otherwise
But it also mutates the seen list.
3. Creating the Filter Function
f = make_filter(2)
This means:
limit = 2
seen = [] (shared list)
f is now:
lambda x: x > 2 and not seen.append(x)
4. Applying filter
filter(f, [1,2,3,4,3,5])
5. Why duplicates are not removed
Because:
seen.append(x) is always executed for x > limit.
No check is done to prevent duplicates.
The lambda only tests x > limit.
So every value > 2 passes, including repeated 3.
6. Final Output
print(list(filter(f, [1,2,3,4,3,5])))
Output:
[3, 4, 3, 5]
Final Answer
[3, 4, 3, 5]
.png)

0 Comments:
Post a Comment