Live data from Hacker News

Migrating to Python 3 with pleasure

github.com

51–60 of 181 posts

Re: Migrating to Python 3 with pleasure

#51
post #50
post #32

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 :)

Is the new way still a shallow copy?

Re: Migrating to Python 3 with pleasure

#52
post #9

I'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.

There’s a well known YouTube video which covers all this comprehensively.

Raymond Hettinger, Modern Python Dictionaries... hopefully this is the correct one. https://youtu.be/npw4s1QTmPg

Re: Migrating to Python 3 with pleasure

#53

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…

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.

I vaguely remember that the change to ordered keys in 3.6 was actually a side effect of making the dict implementation more efficient!

https://mail.python.org/pipermail/python-dev/2016-September/...

Re: Migrating to Python 3 with pleasure

#54

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

Unless there is an "order by" clause, the order of rows in the result set is undefined and non-deterministic from query to query.

Re: Migrating to Python 3 with pleasure

#55
post #42
post #39

Earlier 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}

why wouldn't you be able to just keep the same syntax and make it twice as fast in a newer implementation?

Re: Migrating to Python 3 with pleasure

#58

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

Do you mean the columns of the row?

Re: Migrating to Python 3 with pleasure

#60
post #32

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

It also allows for unioning two dicts together:

    baz = {**foo, **bar}
Post reply on HN