Open gbnewb opened 2 years ago
I think Section 11.1 should be updated regarding this.
As I understand it, ordered dictionaries are not part of the Python language specification, they are an implementation detail of some versions of CPython.
The book specifies Python 3 but not which implementation or version. So I don't think I can generally say that dictionaries are ordered.
On Thu, Feb 22, 2024, at 11:26 AM, Joseph McMurry wrote:
I think Section 11.1 should be updated regarding this.
— Reply to this email directly, view it on GitHub https://github.com/AllenDowney/ThinkPython2/issues/95#issuecomment-1959808773, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAOLP3KWPVYB4JIFS22IMCLYU5WTDAVCNFSM5XRWLHZKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOJVHE4DAOBXG4ZQ. You are receiving this because you are subscribed to this thread.Message ID: @.***>
— Allen Downey Probably Overthinking It: probablyoverthinking.it
As of Python 3.7 it's part of the Python language specification. https://docs.python.org/3/whatsnew/3.7.html - "the insertion-order preservation nature of dict objects has been declared to be an official part of the Python language spec."
Also from https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects - "Dictionaries preserve insertion order. Note that updating a key does not affect the order. Keys added after deletion are inserted at the end."
Fair enough -- but still, the book doesn't specify a version, and for the target audience of Think Python, it's a detail we don't need at this point.
On Thu, Feb 22, 2024, at 11:40 AM, Joseph McMurry wrote:
As of Python 3.7 it's part of the Python language specification. https://docs.python.org/3/whatsnew/3.7.html - "the insertion-order preservation nature of dict https://docs.python.org/3/library/stdtypes.html#typesmapping objects has been declared https://mail.python.org/pipermail/python-dev/2017-December/151283.html to be an official part of the Python language spec."
Also from https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects - "Dictionaries preserve insertion order. Note that updating a key does not affect the order. Keys added after deletion are inserted at the end."
— Reply to this email directly, view it on GitHub https://github.com/AllenDowney/ThinkPython2/issues/95#issuecomment-1959837135, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAOLP3KPPT23EPWXQ747KHTYU5YIVAVCNFSM5XRWLHZKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOJVHE4DGNZRGM2Q. You are receiving this because you commented.Message ID: @.***>
— Allen Downey Probably Overthinking It: probablyoverthinking.it
As of Python 3.6, for the CPython implementation of Python, dictionaries remember the order of items inserted. This is considered an implementation detail in Python 3.6; you need to use OrderedDict if you want insertion ordering that's guaranteed across other implementations of Python.
As of Python 3.7, this is a guaranteed language feature, not merely an implementation detail.