2016-06-28 04:05:22 +00:00
|
|
|
.. _polymorphism:
|
|
|
|
|
|
|
|
Polymorphism
|
|
|
|
------------
|
|
|
|
|
|
|
|
Note that none of the bidict types inherit from dict::
|
|
|
|
|
|
|
|
>>> from bidict import bidict
|
|
|
|
>>> isinstance(bidict(), dict)
|
|
|
|
False
|
|
|
|
|
|
|
|
If you must use :func:`isinstance` to check whether a bidict is dict-like,
|
|
|
|
you can use the abstract base classes from the :mod:`collections` module,
|
|
|
|
which is a better way to check for interface conformance::
|
|
|
|
|
|
|
|
>>> from collections import Mapping, MutableMapping
|
|
|
|
>>> isinstance(bidict(), Mapping)
|
|
|
|
True
|
|
|
|
>>> isinstance(bidict(), MutableMapping)
|
|
|
|
True
|
|
|
|
|
|
|
|
Though you can often write more polymorphic code
|
|
|
|
by using duck typing rather than :func:`isinstance`::
|
|
|
|
|
|
|
|
>>> # LBYL-style:
|
|
|
|
>>> if hasattr(foo, '__setitem__'): # doctest: +SKIP
|
squashed changes for 0.13.0
- support Python 3.6, refactor CI/test setup, increase test coverage
- refactor BidirectionalMapping, BidictBase, OrderedBidictBase,
FrozenBidictBase, and subclasses
- move frozenorderedbidict into _frozen and looseorderedbidict into _loose
- register bidict as a virtual subclass of MutableMapping rather than
inheriting from it directly. This makes it clearer that it does not use any
of the concrete generic methods that MutableMapping provides.
- improve performance and flexibility of frozenbidict and
frozenorderedbidict hashing
- docs, including new type-hierarchy.png diagram
- rm unused imap, ifilter, izip_longest from compat, add PYPY
- update to latest versions of dependencies
- restore benchmarking on travis
2017-01-09 15:37:31 +00:00
|
|
|
... foo['bar'] = 'baz'
|
2016-06-28 04:05:22 +00:00
|
|
|
>>> # EAFP-style:
|
|
|
|
>>> try: # doctest: +SKIP
|
squashed changes for 0.13.0
- support Python 3.6, refactor CI/test setup, increase test coverage
- refactor BidirectionalMapping, BidictBase, OrderedBidictBase,
FrozenBidictBase, and subclasses
- move frozenorderedbidict into _frozen and looseorderedbidict into _loose
- register bidict as a virtual subclass of MutableMapping rather than
inheriting from it directly. This makes it clearer that it does not use any
of the concrete generic methods that MutableMapping provides.
- improve performance and flexibility of frozenbidict and
frozenorderedbidict hashing
- docs, including new type-hierarchy.png diagram
- rm unused imap, ifilter, izip_longest from compat, add PYPY
- update to latest versions of dependencies
- restore benchmarking on travis
2017-01-09 15:37:31 +00:00
|
|
|
... foo['bar'] = 'baz'
|
2016-06-28 04:05:22 +00:00
|
|
|
... except TypeError:
|
|
|
|
... # plan B
|