Live data from Hacker News

What’s New in Python 3.7

docs.python.org

61–70 of 70 posts

Re: What’s New in Python 3.7

#61

Earlier quoted context omitted.

It makes sense in context. {} is a dict literal, and * * dict is already used other places to expand a dict into key-value pairs. Given a = {1: 1} b = {2: 2} This c = {**a, **b} is equivalent to c = {1: 1, 2: 2}

a|b would be the obvious choice, because it already works for set: {1,2}|{2,3} == {1,2,3} I guess the reason they went for the weird {∗∗a, ∗∗b} syntax is that it is not a commutative operation.

I guess I didn't explain it clearly enough. ∗∗a is not new syntax, it's just applicable in a new context.

Re: What’s New in Python 3.7

#63

Earlier quoted context omitted.

If it was changed from ascii to utf-8, how does it break everyting else ?

ASCII doesn't exist as such. Characters are encoded in one byte and ascii leaves the highest 128 characters undefined. They are interpreted system-wise, depends on the locale and user settings.

ASCII very much exists - but what you describe is an entierly different thing. Sure - if the change is from default-user-locale to UTF-8, things would break.

But if the changelog is truthful, and the change is from ASCII to UTF-8, things should not break - at least not to any great extent.

Re: What’s New in Python 3.7

#64

Earlier quoted context omitted.

What happens to duplicate keys?

They get overwritten by the value of the dictionary that came last. >>> dict1 = {"key1": 1, "key2": "hello", "key3": [1, 2, 3]} >>> dict2 = {key1": 1, "key2": "world", "key3": [1, 4], "key4": "4"} >>> {**dict1, **dict2} {'key1': 1, 'key2': 'world', 'key3': [1, 4], 'key4': '4'}

Lots of code to show that. How 'bout this: ;-)

  >>> {**{'a': 'first'}, **{'a': 'second'}}
  {'a': 'second'}

Re: What’s New in Python 3.7

#65

Earlier quoted context omitted.

Why add that crazy syntax when a + would be a lot clearer?

It makes sense in context. {} is a dict literal, and * * dict is already used other places to expand a dict into key-value pairs. Given a = {1: 1} b = {2: 2} This c = {**a, **b} is equivalent to c = {1: 1, 2: 2}

Correct. This is called orthogonality. I just used it now to discover that this same syntax works for both sets and lists (using * instead of )

  >>> s1 = {'a', 'b', 'c'}
  >>> s2 = {1, 2, 3}
  >>> {*s1, *s2}
  {'b', 1, 2, 3, 'c', 'a'}

  >>> l1 = ['a', 'b', 'c']
  >>> l2 = [1, 2, 3]
  >>> [*l1, *l2]
  ['a', 'b', 'c', 1, 2, 3]

Re: What’s New in Python 3.7

#66

Unrelated to python 3.7, but they recently added this feature that you can add two dictionaries together in 3.5 by doing: new_dict = {**dict1, **dict2} It is so handy and nice

It's fun that I use it every day but my linter still thinks is a syntax error :)

A linter for a dynamic language has got to be the hardest thing ever to implement.

In PyCharm if you return from within a contextlib.suppress context it'll think any code following it is unreachable code.

https://youtrack.jetbrains.com/issue/PY-25809 https://youtrack.jetbrains.com/issue/PY-16419

Re: What’s New in Python 3.7

#67
post #62

What version will contain dataclasses?

What do you mean by this? Do namedtuples not give you this?

  >>> class MyClass(namedtuple('_MyClass', ['x', 'y', 'z'])):
  ...   def foo(self):
  ...     return self.x * self.y + self.z
  ... 
  >>> x = MyClass(4, 5, 6)
  >>> x.foo()
  26
... or an attrs decorated class with frozen=True?

  >>> import attr
  >>> @attr.s(frozen=True)
  ... class MyClass:
  ...   x = attr.ib()
  ...   y = attr.ib()
  ...   z = attr.ib()
  ...   def foo(self):
  ...     return self.x * self.y + self.z
  ... 
  >>> x = MyClass(4, 5, 6)
  >>> x.foo()
  26

Re: What’s New in Python 3.7

#68
post #62

What version will contain dataclasses?

What do you mean by this? Do namedtuples not give you this? >>> class MyClass(namedtuple('_MyClass', ['x', 'y', 'z'])): ... def foo(self): ... return self.x * self.y + self.z ... >>> x = MyClass(4, 5, 6) >>> x.foo() 26 ... or an attrs decorated class with frozen=True? >>> import attr >>> @attr.s(frozen=True) ... class MyClass: ... x = attr.ib() ... y = attr.ib() ... z = attr.ib() ... def foo(self): ... return self.x…

Close! ericvsmith, gvr, et al were inspired by attrs and have since been implementing a stdlib version of it, calling it "dataclass"

see: https://github.com/ericvsmith/dataclasses

and this comment thread is revealing: https://github.com/ericvsmith/dataclasses/issues/19

Re: What’s New in Python 3.7

#69
post #60
post #16

Earlier quoted context omitted.

Isn't ASCII a strict subset of UTF-8? If anything this should make sure programs don't break because they read some utf-8 extended character when expecting just ascii -- which is the most common case.

I didn't say it's a bad change! But since you raise the issue, there are downsides: Many programs developed on 3.7 will inadvertently break on pre-3.7 Python versions. As we know, this is a big thing on parts of the Python ecosystem because users frequently rely on the Python version that come with their OS or framework (eg AWS Lambda), and many developers want to stay compatible with the most common platform-shipped…

It's a good assumption for 3.7. In isolation this is a pragmatic decision and makes sense.

You are correct though. A .py file which uses non-ascii and doesn't declare the encoding should be considered an error though. Hopefully everybody's dev tools will pick this up (even on 3.7 where they wouldn't need to)

Re: What’s New in Python 3.7

#70
post #68

Earlier quoted context omitted.

What do you mean by this? Do namedtuples not give you this? >>> class MyClass(namedtuple('_MyClass', ['x', 'y', 'z'])): ... def foo(self): ... return self.x * self.y + self.z ... >>> x = MyClass(4, 5, 6) >>> x.foo() 26 ... or an attrs decorated class with frozen=True? >>> import attr >>> @attr.s(frozen=True) ... class MyClass: ... x = attr.ib() ... y = attr.ib() ... z = attr.ib() ... def foo(self): ... return self.x…

Close! ericvsmith, gvr, et al were inspired by attrs and have since been implementing a stdlib version of it, calling it "dataclass" see: https://github.com/ericvsmith/dataclasses and this comment thread is revealing: https://github.com/ericvsmith/dataclasses/issues/19

Thanks for that link. Very informative interesting discussion. There must be a lot of desire for attrs from the stdlib developers.
Post reply on HN