Django uses metaclasses

Your free e-book!

See when it is not worth using Scrum.
"Why Scrum Doesn't Work" Download

Unpacking what’s in Python 3.5

Python 3.5 – the latest major release of my favorite programming language comes with a few handy additions – one of which is a small new convenience feature I wanted to write about today. Among definitely more complex enhancements like async/await, type hinting support, Windows C-runtime and installer changes, it might be easily overlooked. Still, I think python is often perceived as a very succinct and readable language – and the inclusion of those unpacking generalizations make it even more so, and deserve a short article.

What’s new

The new feature – described in detail in PEP 0448 is really simple. You’re probably familiar with functions taking variable number of either positional of keyword arguments:

def positional(*args):  
def keywords(**kwargs):  
    # tip for the uninitiated - kwargs stands for keyword arguments.
def any_arg_combination(*args, **kwargs):  

It’s all cool if you call those functions while providing arguments explicitly:

positional(1, 2, 3)  
keywords(a=1, b=2, c=3)  
any_arg_combination(1, 2, 3, a=1, b=2, c=3)  

will output (notice that positional arguments are tuples, which are immutable, whereas keywords are mutable dicts):

(1, 2, 3)
{'b': 2, 'a': 1, 'c': 3}
(1, 2, 3)
{'b': 2, 'a': 1, 'c': 3}

But suppose that we are not operating on explicit arguments, but we’ve already received a tuple or dictionary, and want to pass it forward with some modification – this is not an exceptionally rare problem – it can occur for example when writing function decorators. In case of positional arguments we can, just stick it in front:

args = (1, 2, 3)  
positional(0, *args)  

If the order is important though, we’re out of luck, prior to python 3.5, trying to call:

positions(*args, 4)  
>>> SyntaxError: only named arguments may follow *expression

To make it work as desired, we would have to turn the received args tuple into a mutable object (possibly list), append to it, and then pass it forward, or use itertools.chain – both solutions adding boilerplate code. In python 3.5 though, this will just work as expected:

positions(*args, 4)  
(1, 2, 3, 4)

It is also possible to have arbitrary number of unpacking operations in a function call:

positional(*args, *args)  
(1, 2, 3, 1, 2, 3)
positional(*args, 4, 5, *args, 6)  
(1, 2, 3, 4, 5, 1, 2, 3, 6)

This also works in assignments:

al1 = [1, 2, 3]  
al2 = [4, 5]  
a = *al1, *al2  
(1, 2, 3, 4, 5)

In 3.4 we’ll get:

SyntaxError: can use starred expression only as assignment target  

The dictionary unpacking changes are in similar fashion. Previously, if you wanted to combine two dictionaries, you had to either call the update method, or use collections.ChainMap. Now it just works, and corner cases seem to follow the principle of least surprise:

ad1 = {1:11, 2:22, 3:33}  
TypeError: keywords() keywords must be strings

ad1 = {'1':11, '2':22, '3':33}  
{'1': 11, '3': 33, '2': 22}

ad2 = {'1':11, '4':22, '5':33}  
keywords(**ad1, **ad2)  
TypeError: keywords() got multiple values for keyword argument '1'

ad2 = {'4':22, '5':33}  
keywords(**ad1, **ad2)  
{'1': 11, '5': 33, '2': 22, '3': 33, '4': 22}

One limitation that is still valid, is that you cannot mix the order of iterable and keyword arguments:

any_arg_combination(*al1, **ad1, *al2, **ad2)  
SyntaxError: iterable argument unpacking follows keyword argument unpacking  

This does not seem like a needed feature though.

Software Engineers - development team


There you have it. The addition is really small, but I find it convenient, and I am under the impression that the small features like this combined together are actually a crucial part of the overall Python programming experience. Not enough attention is being given to them (e.g: I only learned about Python 3 keyword only arguments syntax a week ago, while it was around since python 3.0) – hence, this article. Hope you enjoyed the read.

cto - Chris Gibas

Free 30-minute consultation with our CTO

Chris Gibas - our CTO will be happy to discuss your project! Let's talk!

More blog posts
What is app modernization, and when should you consider it?


What is app modernization, and when should you consider it?

Do you feel like your business applications are no longer enough for your organization? It may be the right time for you to consider app modernization. You can improve your business solutions performance and leverage the most popular technical innovations. From this article, you will learn what it means to modernize applications and when you should do it.   Some companies […]

Digital transformation in business – why do you need this for your company?

How can you make your company more efficient, reduce costs and improve the quality of your customer service? Have you ever heard about digital transformation in business? Move from paper documentation to digital systems, adopt new technologies and optimize your business processes to modernize your organization. You can increase profits and make everyday work easier. Read, to learn more.  How […]

Digital transformation in business – why do you need this for your company?


UX design for web applications: TOP 5 best practices


UX design for web applications: TOP 5 best practices

Which elements of UX design in the web application would you define as most important? What is the difference between a functional and a successful web app? These are only some of the questions that you should ask yourself before you start the development of your application. CEach web application has some goal (or goals) — selling products, entertaining or […]

Benefits of Artificial Intelligence in banking

The vital role of Artificial Intelligence in banking solutions development is undeniable. Why are ML and AI so important in this industry? Learn more about the current state of AI in banking and the benefits of using AI in banking software development. Check it out, if you are considering the application of AI in your business. Adopting AI-based solutions enables […]

Benefits of Artificial Intelligence in banking


Get a free estimation

Need a successful project?