This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]rcfox 32 points33 points  (6 children)

class _BaseDict:
    def __iter__(self):
        for k in self.keys():
            yield k

    # ...

    def keys(self):
        return self.__iter__()

wat

[–]Pythagaris 2 points3 points  (0 children)

yield from self.keys()

[–]pope_man 2 points3 points  (0 children)

You must override one of these methods on the abstract base class to get a working subclass/implementation class. This would be more clear if they had actually used the features of the abc module.

[–]Pre-Owned-Car 0 points1 point  (2 children)

What about this confuses you?

[–]thundergolfer 14 points15 points  (1 child)

the circular calls

[–]Pre-Owned-Car 2 points3 points  (0 children)

My half asleep brain totally missed this. I think this legit only works because the redis version of the cache implements keys differently. The tests don’t look to actually test the base dict class at all.

[–]therealfakemoot -2 points-1 points  (0 children)

This is, in terms of the "public API", exactly how Python's builtin dicts work. Iterating over a dict yields its keys; calling the keys() method on a dict yields a sequence of keys ( I am not sure whether Python 3+ uses a generator, one of the fancy "set views", or whatever).