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.
What’s New in Python 3.7
61–70 of 70 posts
Re: What’s New in Python 3.7
#62Re: What’s New in Python 3.7
#63Earlier 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.
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
#64Earlier 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'}
>>> {**{'a': 'first'}, **{'a': 'second'}}
{'a': 'second'}Re: What’s New in Python 3.7
#65Earlier 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}
>>> 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
#66Unrelated 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 :)
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
#67What version will contain dataclasses?
>>> 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()
26Re: What’s New in Python 3.7
#68What 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…
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
#69Earlier 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…
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
#70Earlier 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