Live data from Hacker News

Migrating to Python 3 with pleasure

github.com

41–50 of 181 posts

Re: Migrating to Python 3 with pleasure

#41

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

Indeed, but it's much nicer when it's universal and built-in rather than requiring a specific import and different, more cumbersome syntax.

Re: Migrating to Python 3 with pleasure

#42
post #39
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

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

#43
post #14
post #4

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

You can use some backports of libraries though, surely? `pathlib` is in pip.

Re: Migrating to Python 3 with pleasure

#45
post #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.

I was thinking of this - https://stackoverflow.com/questions/34142485/difference-betw...

Re: Migrating to Python 3 with pleasure

#46

Is there some kind of slash operator making this statement work? Is this a way to concatenate things? train_path = datasets_root / dataset / 'train'

pathlib[1] has been in the standard library since Python 3.4. It can do this.[2]

[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

#47

Is there some kind of slash operator making this statement work? Is this a way to concatenate things? train_path = datasets_root / dataset / 'train'

Python allows the overriding of just about every operator. For Pathlib they overrode the division operator to instead perform path addition in a platform agnostic manner.

Re: Migrating to Python 3 with pleasure

#48

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

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.

Re: Migrating to Python 3 with pleasure

#49

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

Would you expect that `str(some_dict) == str(some_dict)`?

Re: Migrating to Python 3 with pleasure

#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 :)
Post reply on HN