
On 9 January 2016 at 16:03, Serhiy Storchaka <storchaka@gmail.com> wrote:
On 08.01.16 23:27, Victor Stinner wrote:
Add a new read-only ``__version__`` property to ``dict`` and ``collections.UserDict`` types, incremented at each change.
This may be not the best name for a property. Many modules already have the __version__ attribute, this may make a confusion.
The equivalent API for the global ABC object graph is abc.get_cache_token: https://docs.python.org/3/library/abc.html#abc.get_cache_token One of the reasons we chose that name is that even though it's a number, the only operation with semantic significance is equality testing, with the intended use case being cache invalidation when the token changes value. If we followed the same reasoning for Victor's proposal, then a suitable attribute name would be "__cache_token__".
The C code uses ``version++``. The behaviour on integer overflow of the version is undefined. The minimum guarantee is that the version always changes when the dictionary is modified.
For clarification, this code has defined behavior in C (we should avoid introducing new undefined behaviors). May be you mean that the bahavior is not specified from Python side (since it is platform and implementation defined).
At least in recent versions of the standard*, overflow is defined on unsigned types as wrapping modulo-N. It only remains formally undefined for signed types. *(I'm not sure about C89, but with MSVC getting their standards compliance act together, we could potentially update our minimum C version expectation in PEP 7 to C99 or even C11).
Usage of dict.__version__ =========================
This also can be used for better detecting dict mutating during iterating: https://bugs.python.org/issue19332.
I initially thought the same thing, but the cache token will be updated even if the keys all stay the same, and one of the values is modified, while the mutation-during-iteration check is aimed at detecting changes to the keys, rather than the values. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia