Python has this neat feature of unpacking tuples during item assignment. Here’s a general example:

or if you have a function that returns multiple items:

But what if (for some reason) you have a function that returns some large number of variables (or a smaller number of long-named variables) and need to unpack all of them? How do we style this for PEP8 compliance and maintaining readability?

The Ugly

This is the naive approach, and also breaks line-length style.

The Good

This is probably the best style for this sort of thing (indentation style-dependent)

The Bad

Don’t do this. No matter how tempted you are. This is bad.

The Worse (Halloween Edit!)

Here’s a followup, even worse way to do things…

Let’s assume we have some function called foo that returns some number of variables. For testing purposes we’ll set this function to just return all of its arguments.

We can call this and bind each of the returned variables to a custom variable in the locals() or globals() namespace (or both if we really want).

Which works great!

Leave a Reply

Your email address will not be published. Required fields are marked *