Django uses metaclasses
back

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):  
    print(args)
def keywords(**kwargs):  
    # tip for the uninitiated - kwargs stands for keyword arguments.
    print(kwargs)
def any_arg_combination(*args, **kwargs):  
    print(args)
    print(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  
a  
(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}  
keywords(**ad1)  
TypeError: keywords() keywords must be strings

ad1 = {'1':11, '2':22, '3':33}  
keywords(**ad1)  
{'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

Conclusion

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
Decentralized Finance (DeFi) – what is it?

Oleksandra Bilokrys

Decentralized Finance (DeFi) – what is it?

Decentralized Finance, in short DeFi, is a concept which enables entrepreneurs who choose to take advantage of blockchain technology and traditional financial instruments in a decentralized architecture. Two examples of DeFi applications are Bitcoin and Etherum. They are both controlled by big networks of computers and not central authorities. What does that mean and why is DeFi becoming more and […]

What are smart contracts and what is their potential for business?

Are you interested in blockchain technology, and you are wondering what its business potential is? To understand the possible application in your industry, you first need to learn how it works. One of the most useful tools associated with blockchain is smart contracts. They enable transferring not only fiat currency and cryptocurrencies but also other resources. Smart contracts are applications […]

What are smart contracts and what is their potential for business?

Idego

Substantive support - Oleksandra Bilokrys

How to augment your software development team? Staff augmentation in practice.

Idego

Substantive support - Oleksandra Bilokrys

How to augment your software development team? Staff augmentation in practice.

Do you need high-quality software for your company? In some situations only high-quality, custom solutions can solve certain business problems, but how to develop the right software? No one better than you knows what exactly you need to become more efficient and thus, more competitive. If you do not have enough experienced employees for such a big IT undertaking in […]

Hiring the best machine learning developer – what do you need to know?

Finding and hiring specialists for your IT department is never easy, especially when your company is taking advantage of quite complex IT solutions for business. Do you need to employ the best machine learning developer available on the market? A deep understanding of the IT field – which is a data-based science – is crucial for HR experts and managers […]

Hiring the best machine learning developer – what do you need to know?

Idego

Substantive support - Oleksandra Bilokrys

Get a free estimation

Need a successful project?