I said earlier that deep unpacking can also help you find bugs in your code. You can use deep unpacking in a for loop (and in a list comprehension too): colours = [ Now imagine that you have a list with some colours and want to compute the greyscales. You could have defined your function to be def greyscale(r, g, b):īut sometimes you are writing code that interacts with other people's code,Īnd sometimes there are already types and formats of data that are in use,Īnd it is just simpler to adhere to whatever the standards are. To take the separate r, g, and b values as arguments from the get-go.”. “That is all well and good, but you could have just defined the function Of course, more cunning or suspicious readers might say Print(greyscale(colour)) # still prints 246.8046 Mathematical formula I showed: def greyscale(colour_info): The formula will be spelled out pretty much like if it were the original In fact, if we use deep unpacking to extract the r, g, and b values, The long formula with the additions and multiplications doesn't look very nice. Print(greyscale(colour)) # prints 246.8046īut I think we can all agree that the function definition could surely be improved. Now you can use your function: colour = ("AliceBlue", (240, 248, 255)) Slightly more involved formula, but it will be good enough for our purposes.) We could write a function that takes the colour information like we have been using,Īnd then computes its greyscale value: def greyscale(colour_info): Which weighs the R, G, and B components differently. Given the RGB values of a colour, you can apply a basic formula to convert it to greyscale, 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 parentheses that Our use of parentheses 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)) 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, > # 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |