I'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
This is a lot like JavaScript :)
Migrating to Python 3 with pleasure
51–60 of 181 posts
Re: Migrating to Python 3 with pleasure
#52I've moved to python 3 over the past couple of months, after resisting for the better part of a decade. I like it. One surprising thing I learned from this document is that dicts now iterate in assignment order, not hash order. That's going to break some code for people.
Dicts never had an iteration order, though. If you relied on dicts iterating in order, you were kind of asking for it.
Raymond Hettinger, Modern Python Dictionaries... hopefully this is the correct one. https://youtu.be/npw4s1QTmPg
Re: Migrating to Python 3 with pleasure
#53Earlier 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…
I have the opposite reaction to it, it seems insanely idiotic to have a associative array with ordered keys. It can only make sense to someone who doesn't know anything about fundamental data structures and a language that caters to people like that in spite of the performance penalty is just strange. but hey, its Guido, I still can't fathom that he moved reduce into functools.
https://mail.python.org/pipermail/python-dev/2016-September/...
Re: Migrating to Python 3 with pleasure
#54Earlier quoted context omitted.
>>(all database query result rows, for one) What? No. SQL does not return results in any consistent ordering unless specifically instructed to.
Not the result set, the rows of the result set.
Re: Migrating to Python 3 with pleasure
#55Earlier quoted context omitted.
return {**old, 'foo': 'bar'} # Old way return dict(old, foo='bar') Not much difference if you ask me.
It's twice as slow, doesn't work with more than one dictionary, you can't easily control the merge prescience and you can't (easily) use expressions/variables in the keys: {**x, 'fo'+'o': 'bar', **y}
Re: Migrating to Python 3 with pleasure
#56Re: Migrating to Python 3 with pleasure
#57I'm not sure why this is targeted toward data scientists; these tips are useful for any Python user.
Re: Migrating to Python 3 with pleasure
#58Re: Migrating to Python 3 with pleasure
#59Re: Migrating to Python 3 with pleasure
#60I'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
baz = {**foo, **bar}