Wednesday, November 9, 2016

Goodies in Python 3.6!

Python 3.6 is around the corner!

Reading the new features I am excited to see these:

Formatted string literals.

>>> name = "Fred"
>>> f"He said his name is {name}."
'He said his name is Fred.'
>>>

The dict type now uses a “compact” representation pioneered by PyPy. The memory usage of the new dict() is between 20% and 25% smaller compared to Python 3.5. Dictionaries are now ordered!

Python 3.6.0b4 (v3.6.0b4:18496abdb3d5, Nov 21 2016, 20:44:47)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def f(**kwargs):
...     print(kwargs)
...
>>> f(a=1, b=2, c=3, d=4)
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>> {'a': 1, 'b': 2, 'c': 3, 'd': 4}
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>> d = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>> d.keys()
dict_keys(['a', 'b', 'c', 'd'])
>>> d.values()
dict_values([1, 2, 3, 4])
>>> d.items()
dict_items([('a', 1), ('b', 2), ('c', 3), ('d', 4)])
>>>

Import now raises the new exception ModuleNotFoundError (subclass of ImportError) when it cannot find a module. Code that current checks for ImportError (in try-except) will still work.


No comments:

Post a Comment