top | item 33357710

(no title)

kotlin2 | 3 years ago

    c = defaultdict(lambda: 42)
    b = defaultdict(lambda: c)
    a = defaultdict(lambda: b)
    a["a"]["b"]["c"]  # --> 42

discuss

order

Spivak|3 years ago

Okay fair, I deserve that. I assumed it was obvious I meant arbitrary depth.

Also d["a"] and d["a"]["b"] aren't 42.

kotlin2|3 years ago

If d["a"]["b"] is 42, then how could d["a"]["b"]["c"] also be 42? What you want doesn't make sense semantically. Normally, we'd expect these two statements to be equivalent

d["a"]["b"]["c"] == (d["a"]["b"])["c"]

Too|3 years ago

The MagicMock class from unittest package does what you want.

I have a hard time understanding any use case outside of such mocking.