Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22462

Caktus Consulting Group: New year, new Python: Python 3.6

$
0
0

Python 3.6 was released in the tail end of 2016. Read on for a few highlights from this release.

New module: secrets

Python 3.6 introduces a new module in the standard library called secrets. While the random module has long existed to provide us with pseudo-random numbers suitable for applications like modeling and simulation, these were not "cryptographically random" and not suitable for use in cryptography. secrets fills this gap, providing a cryptographically strong method to, for instance, create a new, random password or a secure token.

New method for string interpolation

Python previously had several methods for string interpolation, but the most commonly used was str.format(). Let’s look at how this used to be done. Assuming 2 existing variables, name and cookies_eaten, str.format() could look like this:

"{0} ate {1} cookies".format(name, cookies_eaten)

Or this:

"{name} ate {cookies_eaten} cookies".format(name=name, cookies_eaten=cookies_eaten)

Now, with the new f-strings, the variable names can be placed right into the string without the extra length of the format parameters:

f"{name} ate {cookies_eaten} cookies"

This provides a much more pythonic way of formatting strings, making the resulting code both simpler and more readable.

Underscores in numerals

While it doesn’t come up often, it has long been a pain point that long numbers could be difficult to read in the code, allowing bugs to creep in. For instance, suppose I need to multiply an input by 1 billion before I process the value. I might say:

bill_val = input_val * 1000000000

Can you tell at a glance if that number has the right number of zeroes? I can’t. Python 3.6 allows us to make this clearer:

bill_val = input_val * 1_000_000_000

It’s a small thing, but anything that reduces the chance I’ll introduce a new bug is great in my book!

Variable type annotations

One key characteristic of Python has always been its flexible variable typing, but that isn’t always a good thing. Sometimes, it can help you catch mistakes earlier if you know what type you are expecting to be passed as parameters, or returned as the results of a function. There have previously been ways to annotate types within comments, but the 3.6 release of Python is the first to bring these annotations into official Python syntax. This is a completely optional aspect of the language, since the annotations have no effect at runtime, but this feature makes it easier to inspect your code for variable type inconsistencies before finalizing it.

And much more…

In addition to the changes mentioned above, there have been improvements made to several modules in the standard library, as well as to the CPython implementation. To read about all of the updates this new release includes, take a look at the official notes.


Viewing all articles
Browse latest Browse all 22462

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>