![]() ![]() 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. 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 However, IMO, this is better than the hack of using using multiple expressions in a tuple and returning the last one.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 This is just to suggest that it can be done, and should not be taken as a recommendation. Print(min(points, key=lambda y: next(b for (a,b),c in ))) Print(min(points, key=lambda p: (lambda a,b: (lambda x,y: (y))(*a))(*p)))Īlternatively one can also use a list instead of a tuple. In comparison to the accepted answer using an unwrapper lambda: values = ![]() Print(min(values, key=lambda y: next(b for ((a,b),c) in (y,)))) In comparison with the accepted answer of using a wrapper, this solution is able to completely destructure the arguments while the wrapper only destructures the first level. Print(min(points, key=lambda y: next(x*x + y*y for (x,y) in ))) It is possible to abuse it to obtain similar behavior in Python 3. While the destructuring arguments was removed in Python3, it was not removed from comprehensions. Still, there are possible uses - if there are various one-liners that would operate on this point, it could be worth to have a namedtuple, and use the assignment expression to effectively "cast" the incoming sequence to the namedtuple: > from collections import namedtuple One should keep in mind that this kind of code will seldom be more readable or practical than having a full function. So, if one uses a trick to execute multiple expressions inside a lambda - I usually do that by creating a tuple and just returning the last component of it, it is possible to do the following: > a = lambda p:(x:=p, y:=p, x ** 2 + y ** 2) Since the release of Python 3.8, PEP 572 - assignment expressions - have been available as a tool. The way to go would be to raise this issue on the Python ideas mailing list, but be prepared to argue a lot over there to gain some traction.Īctually, just not to say "there is no way out", a third way could be to implement one more level of lambda calling just to unfold the parameters - but that would be at once more inefficient and harder to read than your two suggestions: min(points, key=lambda p: (lambda x,y: (x*x + y*y))(*p))
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |