


It wasn’t until the evening, when I was on my daily walk, that gears began to turn in my brain and I thought about how you might actually do this. We all had a laugh, briefly discussed what dark magic it might take to pull this off, and then conversation moved on. Kathy: You should be able to use that as tuple unpacking Here’s a conversation from a group chat last week (shared with permission): The details of tuple unpacking could be a whole other blog post here it’s sufficient to know that this feature exists and you need to balance both sides for it to work. ValueError: too many values to unpack (expected 3) For example: > longitude, latitude, altitude = 51.9, -0.2 The structures longitude, latitude and 51.9, -0.2 are both Python tuples – the parentheses you often write around tuples are optional.įor tuple unpacking to work, you need to have the same number of variables on the left-hand side as elements on the right, so they can be paired up together. Many programming languages support parallel assignment, which allows you to set multiple variables at once:ĭef get_position (): return 51.9, - 0.2 longitude, latitude = get_position ()Īnother name for this is tuple unpacking.

Image by Kristopha Hohn on Flickr, used under CC BY-SA. Sensibility says overusing reflection will bring seven years of filthy looks from everybody who has to maintain your code. Superstition says breaking a mirror will bring seven years of bad luck. You can skip to the end if you just want the practical lessons. You might find it interesting anyway, but this is probably more niche than my posts. That said, even bad ideas can have interesting things to tell us, so in this post I’m going to explain how it works and how I wrote it.Īttention conservation notice: I had fun trying this idea, and writing out the notes was a useful exercise, but I don’t know how much other people will get out of it. It needs reflection and the exec() function to work, and should never be used for anything serious. say that you have a function to get the min and max values of a tupleĭef minmax(items): return min(items), max(items) min, max = minmax(items) # ALL PASSED def test_minmax_tupple(): input = 1, 2, 3, 4, 5 result = minmax(input) assert result = (1, 5) def test_minmax_min(): input = 1, 5, 7 resultMin = minmax(input) assert resultMin = 1 def test_minmax_max(): input = 1, 33, 99 resultMax = minmax(input) assert resultMax = 99 def test_minmax_min_unpacked(): input = 1, 5, 7 resultMin, resultMax = minmax(input) assert resultMin = 1 def test_minmax_max_unpacked(): input = 1, 33, 99 resultMin, resultMax = minmax(input) assert resultMax = 99 3.Making that tweet work requires a horrific misuse of tuple unpacking, a feature that is usually used for sensible things.`total, k = 0, 1` unpacks the ordered pair `(0,1)` into `total = 0` and `k = 1`ĭef sum_naturals(n): total, k = 0, 1 #SEE ABOVE while k > pair = (1,2) > pair (1,2) > x,y = pair > x 1 > y 2 2.this is not only with a simple binding of literal values in a function that calculates the running total of natural numbers from a → b.
TUPLE UNPACKING CODE
If in doubt, reach out to Multiple assignment and tuple unpacking improve Python code readability - Trey Hunner 1. To decrypt the definition, look for types and examples below. The aim of this tutorial🔍 is to cover the subject of Tuple Unpacking in Python, defined asĭestructuring operation unpacking data structures into named references.
