What’s new in Python 3.6
and all the exciting features…
So, Python3.6 is out with some exciting new features. The new version (3.6) contains 16 PEPs (link down below) there hasn’t been such a big change since the shift to Python3, so with no further ado, here are the things that I am most excited about and why:
You can now yield, asynchronously
Yay, PEPs 525–530 introduce more asynchronous stuff!
Now users can use async and await to define asynchronous generators (a limitation in 3.5 was that one could not use await and yield in the same body).
Jinja2 to fully support the async and await keywords on 3.6 and later
And they can also be used in comprehensions like this:
responses = [await hdlr() for hdlr in handlers if await ready()]
Note that this will only work inside an async def function, otherwise you’ll get a SyntaxError ;) (this restriction is planned to be removed for Python3.7)
String and numeric literals
- A new beautiful way of formatting strings, f-strings, is introduced. It is faster than using the str.format() that you might be already familiar with, requires a minimal syntax and enables using expressions inside of the string:
def convert_cost(items):
return 10 * itemsitems = 2
response = f'The cost is {convert_cost(items)}'print(response)
With this example the output would be:
The cost is 20
Also, note that the f-string is evaluated at runtime (in the example below changing the value of items later on will not change the value of response).
- Numeric literals can now use underscore! Yay! Alright, it’s probably not a huge change but it makes things look much neater. Now you can write:
monthly_salary = 1_000_000_000
binary_stuff = 0b_1001_0111_1010
instead of :
monthly_salary = 1000000000
binary_stuff = 0b100101111010
Nobody’s got time to count them zeros anyway :p
Type Hints
You’re probably familiar with the use of type annotations in function arguments in version 3.5.
Version 3.6 provides a more flexible syntax for variables:
level : int # notice that level is not yet defined
This only annotates level. In fact the name level is not even defined yet.
It might seem like a small change will largely impact the way we use Python
However, if you use the following:
var : str
var = 100
b : int = 'chocolate'
You will simply define a variable var, that contains 100 and a variable b that is a string containing ‘chocolate’
Honorable Mentions
Other changes include:
- Ordered mapping of the keyword arguments kwargs.
- Dictionaries now have a version id.
- Oh, did I mention? pyvenv is deprecated :’( But it does make sense and obliges the developer to know which interpreter they’re working on.
If you haven’t yet, I highly recommend watching Brett Canon’s presentation on PyCon Canada, it explains the changes in more detail and explores the thinking behind them. Also, you can find a link to the Python PEPs over here.
Thanks for reading! :) If you enjoyed it, hit that heart button below. Would mean a lot to me and it helps other people see the story.
Wanna check out a tutorial on how you can make a chatbot on Slack using Python, you can find it over here.
And for more exclusive material, you can join my mailing list by signing up over here. Peace out :D