Live data from Hacker News

Migrating to Python 3 with pleasure

github.com

11–20 of 181 posts

Re: Migrating to Python 3 with pleasure

#11

the Path thing is incredible! I wasn't aware of it. I'll definitively be replacing my os.path.join calls for a more readable version of it.

> test_path = datasets_root / dataset / 'test'

> Previously it was always tempting to use string concatenation (concise, but obviously bad), now with pathlib the code is safe, concise, and readable.

This is the kind of feature that I'm wary to use even in scripts: questionable benefit, and probably too clever.

Re: Migrating to Python 3 with pleasure

#12
post #9

Earlier quoted context omitted.

Dicts never had an iteration order, though. If you relied on dicts iterating in order, you were kind of asking for it.

I believe newer Python versions have at least deterministic iteration order (correct me if I'm wrong), while some previous ones had a non-deterministic one (non-deterministic between mutiple invocations of the interpreter). But there is also OrderedDict which iterates in assignment order.

No, you're right. I believe dict orders were made deterministic (by insertion order) in 3.6. Before that, it was undefined.

Re: Migrating to Python 3 with pleasure

#13

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

Re: Migrating to Python 3 with pleasure

#14
post #4

the Path thing is incredible! I wasn't aware of it. I'll definitively be replacing my os.path.join calls for a more readable version of it.

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

#16

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

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.

Re: Migrating to Python 3 with pleasure

#17

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 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 was maintained by default.

Re: Migrating to Python 3 with pleasure

#18

Is 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

#19

Earlier quoted context omitted.

I believe newer Python versions have at least deterministic iteration order (correct me if I'm wrong), while some previous ones had a non-deterministic one (non-deterministic between mutiple invocations of the interpreter). But there is also OrderedDict which iterates in assignment order.

No, you're right. I believe dict orders were made deterministic (by insertion order) in 3.6. Before that, it was undefined.

CPython interates in this fashion since 3.6, but it is not part of the Python spec.

Might be a bit contentious but it's not supposed to be relied upon

Re: Migrating to Python 3 with pleasure

#20

the Path thing is incredible! I wasn't aware of it. I'll definitively be replacing my os.path.join calls for a more readable version of it.

> test_path = datasets_root / dataset / 'test' > Previously it was always tempting to use string concatenation (concise, but obviously bad), now with pathlib the code is safe, concise, and readable. This is the kind of feature that I'm wary to use even in scripts: questionable benefit, and probably too clever.

I don't have a machine available right now, but I wonder what happens if two adjacent path elements are integers? Does it perform division instead of path/string concatenation?
Post reply on HN