[issue22766] collections.Counter's in-place operators should return NotImplemented for unsupported types

Ethan Furman report at bugs.python.org
Wed Nov 5 17:04:33 CET 2014


Ethan Furman added the comment:

Raymond declared:
----------------
> Let's be clear.  These are duck-typed methods.  A type check is inappropriate.
> Anything with o.items() is allowed regardless of type.

Wikipedia explains (http://en.wikipedia.org/wiki/Duck_typing):
-------------------------------------------------------------
> In computer programming with object-oriented programming languages, duck typing
> is an alternative to typing. In duck typing, an object's suitability for some
> purpose is determined by the presence of certain methods and properties [...]

I did use an actual 'type' check in one of my exmaples, and that was wrong.

It is possible to do a "duck-type check" with a `hasattr(other, 'items')`.

I don't use Counter myself -- I'll try and find some real-world examples.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue22766>
_______________________________________


More information about the Python-bugs-list mailing list