←back to thread

84 points mkalioby | 2 comments | | HN request time: 0s | source
1. badmintonbaseba ◴[] No.42193401[source]
I would prefer something like `{"name": contains("k")}`, where contains("k") returns an object with a custom __eq__ that compares equal to any string (or iterable) that contains "k". Then you can just filter by equality.

I recently started using this pattern for pytest equality assertions, as pytest helpfully produces a detailed diff on mismatch. It's not perfect, as pytest doesn't always produce a correct diff with this pattern, but it's better than some alternatives.

replies(1): >>42193797 #
2. gpderetta ◴[] No.42193797[source]
Instead of returning an __eq__ object, I have had 'contains' just return a predicate function (that you can pass to filter for example. I guess in practice it doesn't really change much, except that calling it __eq__ is a bit misleading.

A significant advantage is that you can just pass an inline lambda.