AllenDowney / ThinkPython2

LaTeX source and supporting code for Think Python, 2nd edition, by Allen Downey.
Other
2.49k stars 1.65k forks source link

Dict is now ordered. Maybe the book should be updated? #95

Open gbnewb opened 2 years ago

gbnewb commented 2 years ago

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.

mcmwebsol commented 6 months ago

I think Section 11.1 should be updated regarding this.

AllenDowney commented 6 months ago

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

mcmwebsol commented 6 months ago

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."

AllenDowney commented 6 months ago

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