ratmili.blogg.se

Tuple unpacking
Tuple unpacking











tuple unpacking
  1. Tuple unpacking how to#
  2. Tuple unpacking code#

Examples in code Increasing expressiveness Nothing better than showing you some code, so you can see for yourself. It doesn't have to be in explicit assignments with an equals sign!ĭeep unpacking, when used well, can improve the readability of your code –īy removing indexing clutter and by making the intent more explicit –Īnd can help you test your code for some errors and bugs. Print_some_colour_info("AliceBlue", (240, 248, 255))ĭeep unpacking can also be used in the implicit assignments of for loops, Next to the value it is getting, it becomes very clear what values go where: > (name, (r, g, b)) = ("AliceBlue", (240, 248, 255))ĭid you know that in Python 2 you could use deep unpacking in function signatures?įor example, this would be valid Python 2 code: def print_some_colour_info(name, (r, g, b)): Now if we put the left-hand side of the assignment, (name, (r, g, b)), This might be clearer if we actually include the outer set of parenthesis that Our use of parenthesis in (r, g, b) tells Python we actually want to go into ValueError: not enough values to unpack (expected 4, got 2) Have four items to unpack: > colour_info = ("AliceBlue", (240, 248, 255))

tuple unpacking

Think we are trying to do multiple assignment, and would expect colour_info to If we had simply written name, r, g, b = colour_info then Python would Mimicking the shape of the colour_info variable. Notice how we group the r, g, and b variables with () to create a tuple, You could use deep unpacking: > colour_info = ("AliceBlue", (240, 248, 255)) Iterables, you can unpack those iterables at once.įor example, using multiple assignment twice in a row, you could do this: > colour_info = ("AliceBlue", (240, 248, 255))īut if you already know you want to get to the separate RGB values, Is on the right-hand side of an assignment in particular, if there are nested In a similar fashion, deep unpacking allows you to match the shape of what On the right-hand side of an assignment, and get each element into a variable. Multiple assignment allows you to match the length of an iterable, Items the right-hand side will have, but all of them can be storedĭeep unpacking, or nested unpacking, is similar to multiple assignment in a sense. With starred assignment you can tell Python that you are not sure how many Starred assignment, that I covered in depth in this Pydon't,Īllows you to write things like > l = Provided the right-hand side has as many items as the left-hand side expects. With multiple assignment you can assign, well, multiple variables at the same time,

tuple unpacking

> # Multiple assignment unpacks the tuple. > x, y = y, x # Multiple assignment to swap variables. In Python, multiple assignment is what allows you to write things like > x = 3 Let's have a quick look at two other nice features about Python's assignments. Assignmentsīefore showing you how deep unpacking works, Learning about deep unpacking will be very helpful in order to pave the roadįor structural matching, a feature to be introduced in Python 3.10.

Tuple unpacking how to#

  • how to use it to improve code readability and.
  • In this Pydon't we will go over deep unpacking: (If you are new here and have no idea what a Pydon't is, you may want to read the The most out of the structural matching feature that is to be introduced Learning about deep unpacking will also be very important in order to make Deep unpacking (or nested unpacking) provides a more powerful way for you toĭeep unpacking can be used to improve the readability of your code and help













    Tuple unpacking