Skip to content

Commit 78d0180

Browse files
committed
simplify test case with nonlocal
1 parent 5a1783b commit 78d0180

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

Lib/test/test_defaultdict.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -188,20 +188,18 @@ def test_union(self):
188188

189189
def test_factory_conflict_with_set_value(self):
190190
key = "conflict_test"
191-
class Factory:
192-
def __init__(self):
193-
self.count = 0
194-
self.test_dict = None
195-
196-
def __call__(self):
197-
self.count += 1
198-
if self.count == 1:
199-
self.test_dict[key] = "set_value"
200-
return "default_factory_value"
201-
202-
factory = Factory()
203-
test_dict = defaultdict(factory)
204-
factory.test_dict = test_dict
191+
count = 0
192+
test_dict = None
193+
194+
def default_factory():
195+
nonlocal count
196+
nonlocal test_dict
197+
count += 1
198+
if count == 1:
199+
test_dict[key] = "set_value"
200+
return "default_factory_value"
201+
202+
test_dict = defaultdict(default_factory)
205203

206204
self.assertEqual(test_dict[key], "set_value")
207205

0 commit comments

Comments
 (0)