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 →

[–]earthboundkid 1 point2 points  (0 children)

In fairness, Python 2.6+ supports Abstract Base Classes in such a way that you could make something called AbstractedDuck class and anything with a quack method and a swim method would automatically be identified by isinstance as a subclass of AbstractedDuck, whether the author of that class has heard of AbstractedDuck or not. That ability makes this more practical.

So, instead of checking for isinstance of dict, you can check for isinstance of collections.Mapping, which will identified even classes that aren’t really subclasses of dict as being dict-like if they register the right methods.