What’s New in Python 3.7
docs.python.org
docs.python.org
new_dict = {**dict1, **dict2}
It is so handy and nice >>> d = {**{1:2,2:2,3:2}
,**{4:2,5:2,6:2}
,**{7:2,8:2,9:2}}
>>> d
{ 1: 2, 2: 2, 3: 2
, 4: 2, 5: 2, 6: 2
, 7: 2, 8: 2, 9: 2} >>> 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'}https://www.python.org/dev/peps/pep-0448/
EDIT: yeah, nvm, it's what everyone else said.
>>> a = {'x': 1}
>>> b = {'x': 2}
>>> {**a, **b}
{'x': 2}
>>> {**b, **a}
{'x': 1}Seems similar to the spread operator in JavaScript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
Given
a = {1: 1}
b = {2: 2}
This c = {**a, **b}
is equivalent to c = {1: 1, 2: 2} {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. >>> 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]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
dict(dict1, **dict2)Source: https://stackoverflow.com/questions/38987/how-to-merge-two-d...
Added two new opcodes: LOAD_METHOD and CALL_METHOD to avoid instantiation of bound method objects for method calls, which results in method calls being faster up to 20%. (Contributed by Yury Selivanov and INADA Naoki in bpo-26110.)
Searching some unlucky Unicode characters (like Ukrainian capital “Є”) in a string was to 25 times slower than searching other characters. Now it is slower only by 3 times in worst case. (Contributed by Serhiy Storchaka in bpo-24821.)
Fast implementation from standard C library is now used for functions erf() and erfc() in the math module. (Contributed by Serhiy Storchaka in bpo-26121.)
The os.fwalk() function has been sped up by 2 times. This was done using the os.scandir() function. (Contributed by Serhiy Storchaka in bpo-25996.)
Optimized case-insensitive matching and searching of regular expressions. Searching some patterns can now be up to 20 times faster. (Contributed by Serhiy Storchaka in bpo-30285.)
selectors.EpollSelector.modify(), selectors.PollSelector.modify() and selectors.DevpollSelector.modify() may be around 10% faster under heavy loads. (Contributed by Giampaolo Rodola’ in bpo-30014)
edit:
That one around the unlucky Unicode characters is fascinating. https://bugs.python.org/issue24821Due to the way that regex searching is optimised, it trips up because: ".. the lowest byte of the code of Ukrainian capital letter Є (U+0404) matches the highest byte of codes of most Cyrillic letters (U+04xx). There are similar issues with some other scripts ..."
There are human devs who needed this? Or is this a sign that AI bots are now involved in language design?
See https://bugs.python.org/issue18896 and https://bugs.python.org/issue12844 for more information.
Also, Django 1.3 had issues with 255 arguments for named tuples.
To my knowledge there are no "AI Bots" that are involved in language design. Code generation is possible. Also solver aided languages See https://emina.github.io/rosette/
- someone creates a function to do something on a business concept that was not materialized by a class. Think of a bunch (~ 8) of properties that belong together but for some bad reason are moved in separate variables all over the place
- another function has to work on a set of those objects. The dev who implements it thinks it would be a good idea to keep all parameters separate. No we have nproperties * nobjects parameters. The dev use a rigorous naming convention for readability (creatorFirstName, creatorLastName, validatorFirstName, etc)
- now we have multiple sets (creatorFirstName1, 2, 3 ...)
I don't remember the details but there was around three hundred parameters.
edit: formatting
>>> def f(*x): print(x[-1])
>>> f(range(300))
299
And surprisingly, in python 2.7, I was able to define a function that takes more than 255 arguments. But perhaps this only worked because I cheated and used exec. >>> exec("def f(" + ",".join("f" + str(x) for x in range(300)) + "): print(f299)"
>>> f(*range(300))
299 >>> exec("def f(" + ",".join("f" + str(x) for x in range(300)) + "): print(f299)")
>>> f(*range(300))
299
>>> exec("f(" + ", ".join(str(i) for i in range(300)) + ")")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
SyntaxError: more than 255 arguments
When reducing the number of parameters to be invalid for f, the following error is shown: >>> exec("f(" + ", ".join(str(i) for i in range(30)) + ")")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
TypeError: f() takes exactly 300 arguments (30 given)
Which seems a bit paradoxical - "You need 300 arguments" - "No, wait, actually, you can't have more than 255" ... ;)It looks like they either had changed the internals of python 3, making it fail at the definition as a side effect instead of when calling, or used the same underlying logic but purposely chose to issue the exception to potentially catch the problem earlier...
>>> print([1,2,3])
[1,2,3]
>>> print(*[1,2,3])
1 2 3This particular limit was not present in the early Python 2 releases.
When the limit was added, limiting arguments wasn't the goal; instead, it was just an artifact of a patch regarding how keyword arguments and positional arguments we encoded in Python's bytecode scheme.
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.
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 Python versions.
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)
When am I going to be able to name my variable as emojis?
edit: I take that back. Apparently thebunicode allowed in identifers is not unrestricted..... in my defense I don't use unicode identifiers as a rule....
Woooo! That means, at some future time, I'll be able to remove code checks for non-threading Pythons.
>>> 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()
26see: https://github.com/ericvsmith/dataclasses
and this comment thread is revealing: https://github.com/ericvsmith/dataclasses/issues/19