
Le samedi 9 janvier 2016, Serhiy Storchaka <storchaka@gmail.com> a écrit :
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.
It's fine to have a __version__ property and a __version__ key in the same dict. They are different. For a module, it's something like: With moddict = globals(): - moddict.__version__ is the dict version - moddict['__version__'] is the module version Using the same name for different things is not new in Python. An example still in the module namespace: - moddict.__class__.__name__ is the dict class name - moddict['__name__'] is the module name (or '__main__') "Version" is really my favorite name for the name feature. Sometimes I saw "timestamp", but in my opinion it's more confusing because it's not related to a clock.
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).
The C type for version is unsigned (size_t). I hope that version++ is defined but I was too lazy to check C specs for that :-) Does it wrap to 0 on overflow on all architecture (supported by Python)? If not, it's easy to wrap manually: version = (version==size_max) ? 0 : version+1;
Usage of dict.__version__
=========================
This also can be used for better detecting dict mutating during iterating: https://bugs.python.org/issue19332.
Oh, cool. Victor