Earlier quoted context omitted.
Not for much longer, as of 3.7 the ordering is a language feature: https://mail.python.org/pipermail/python-dev/2017-December/1... It's mad that it ever wasn't this way. Mapping-with-ordered-keys is such a useful and pervasive data structure (all database query result rows, for one) that an ordered dictionary should be a fundamental part of a language. It has been so much more pleasant to write python since ordering…
The data structure OrderedDict does what you describe and has been in the stdlib since I think 2.7
Migrating to Python 3 with pleasure
41–50 of 181 posts
Re: Migrating to Python 3 with pleasure
#42I've been toying around with Python 3 and using it for most of my personal/hack projects, but I somehow missed the unpacking improvements: https://www.python.org/dev/peps/pep-0448/ In particular, being able to create an updated copy of a dict with a single expression is pretty cool: return {**old, 'foo': 'bar'} # Old way new = old.copy new['foo'] = ['bar'] return new
return {**old, 'foo': 'bar'} # Old way return dict(old, foo='bar') Not much difference if you ask me.
{**x, 'fo'+'o': 'bar', **y}Re: Migrating to Python 3 with pleasure
#43Earlier quoted context omitted.
Wow, I switched to Python 3 really early on and never knew about pathlib.
I have been conditioned to write code that is 2/3 compatible, that even when I am writing specifically for the PY3 interpreter the code turns out to be a __future__ import away from being valid PY2. I did not think much of them at first, but very recently, f-strings have changed that. I think many people get imprinted with writing the PY3 code using only the features available at the time they switched over.
Re: Migrating to Python 3 with pleasure
#44train_path = datasets_root / dataset / 'train'
Re: Migrating to Python 3 with pleasure
#45Is the np.dot -> @ tip reliable? I thought these were fairly different in practice.
What do you mean? `A.dot(B)` and `A @ B` are the same thing for NumPy arrays. You might be mixing it up with the weirdness of `array` versus `matrix`, but that's totally separate.
Re: Migrating to Python 3 with pleasure
#46Is there some kind of slash operator making this statement work? Is this a way to concatenate things? train_path = datasets_root / dataset / 'train'
[1] https://docs.python.org/3/library/pathlib.html
[2] https://docs.python.org/3/library/pathlib.html#basic-use
Re: Migrating to Python 3 with pleasure
#47Is there some kind of slash operator making this statement work? Is this a way to concatenate things? train_path = datasets_root / dataset / 'train'
Re: Migrating to Python 3 with pleasure
#48Earlier quoted context omitted.
Dicts are UNORDERED associative containers. If youre depending your app on implementation defined behavior, that's on your developpers shoulders. Stuff like that shouldn't pass code review
Not for much longer, as of 3.7 the ordering is a language feature: https://mail.python.org/pipermail/python-dev/2017-December/1... It's mad that it ever wasn't this way. Mapping-with-ordered-keys is such a useful and pervasive data structure (all database query result rows, for one) that an ordered dictionary should be a fundamental part of a language. It has been so much more pleasant to write python since ordering…
but hey, its Guido, I still can't fathom that he moved reduce into functools.
Re: Migrating to Python 3 with pleasure
#49Earlier quoted context omitted.
It is surprising though that the order was non-deterministic, not only undefined. Determinism is something I intuitively expect from read-only operations like iteration.
I think that's a habit you need to shake. If something is undefined, you should intuitively shy away from expecting things from it. If I found myself wondering why some undefined behavior was not consistent, I'd question why I even believed it should be consistent in the first place.
Re: Migrating to Python 3 with pleasure
#50I've been toying around with Python 3 and using it for most of my personal/hack projects, but I somehow missed the unpacking improvements: https://www.python.org/dev/peps/pep-0448/ In particular, being able to create an updated copy of a dict with a single expression is pretty cool: return {**old, 'foo': 'bar'} # Old way new = old.copy new['foo'] = ['bar'] return new