3.6 new features

Posted by Bob on Sat 31 December 2016 in News • 3 min read

Python 3.6 is out for over a week now. In this post some features and pointers. What’s New In Python 3.6 is worth reviewing to get a full overview.

Getting it installed is supersimple. I am on Mac so I just installed the pkg file from the downloads page.

Some highlights

$ python3.6 -VV # verbose version info
Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
  • String formatting:

    >>> name = 'bob'
    >>> print('Hello %s' % name) # not pythonic
    >>> print('Hello {}'.format(name)) # better
    Hello bob
    >>> print(f'Hello {name}') # new in 3.6
    Hello bob
    >>> a = 2
    >>> b = 4
    >>> print(f'Look I even can do math: { 5 * (a + b) }')
    Look I even can do math: 30
    
  • Make big numbers more readable with underscores:

    >>> 1_000_000_000_000_000
    1000000000000000
    
  • Type hints:

    In 3.5 you could do this already for methods:

    >>> sys.version
    '3.5.1 |Anaconda 4.0.0 (x86_64)| (default, Dec  7 2015, 11:24:55) \n[GCC 4.2.1 (Apple Inc. build 5577)]'
    >>> def name(name: str) -> str:
    ...     return 'My name is {}'.format(name)
    ... 
    >>> name('bob')
    'My name is bob'
    

    Now you can 'document' a standalone / instance variable's 'intent' as well:

    >>> price : float = 9.99 # saves comments
    

    'Intent' because nothing stops you from assigning another type:

    >>> name : str = 'bob'
    >>> name = 2
    >>> type(name)
    <class 'int'>
    

    As PEP 484 states:

    While these annotations are available at runtime through the usual annotations attribute, no type checking happens at runtime . Instead, the proposal assumes the existence of a separate off-line type checker which users can run over their source code voluntarily. Essentially, such a type checker acts as a very powerful linter.

  • Asyncio:

    Starting with Python 3.6 the asyncio module is no longer provisional and its API is considered stable.

    I have to yet work with asyncio, will do a future post. It has some nice coverage in Fluent Python. This article by the PyCharm Team shows a nice refactoring using the new Asynchronous Generators feature.

  • New dict implementation:

    The memory usage of the new dict() is between 20% and 25% smaller compared to Python 3.5.

    I also heard that new dicts keep their order, but read now that:

    The order-preserving aspect of this new implementation is considered an implementation detail and should not be relied upon.

    If you need order you can use collections.OrderedDict.

  • New secrets module:

    The main purpose of the new secrets module is to provide an obvious way to reliably generate cryptographically strong pseudo-random values suitable for managing secrets, such as account authentication, tokens, and similar.

    Good to know:

    Note that the pseudo-random generators in the random module should NOT be used for security purposes. Use secrets on Python 3.6+ and os.urandom() on Python 3.5 and earlier.

    Of course random is fine for simulation, but for applications using cryptography, you want to use secrets.

  • The pyvenv script has been deprecated in favour of python3 -m venv (new to Virtual Envs, read our article).

  • Lot of improvements: again, worth reading through the official release doc.

2 or 3?

Unless you are bound to Python 2.x due to environment / requirements, it is a really good time to switch to 3 now. 2.x is not actively developed anymore, all new cool stuff is added to 3 as you can see from this new big release. I use 3.x whenever I can!


Keep Calm and Code in Python!

-- Bob


See an error in this post? Please submit a pull request on Github.

Join our community and grab our Become a Better Python Developer cheat sheet. Learn Python. Receive bonus material. Challenge yourself!