6 ms·
Out of curiosity, what don't you like about Python's packing/unpacking?
by nerdponx 8y ago
Out of curiosity, what don't you like about Python's packing/unpacking?
- Something1234 8y agoIf I had to say anything I would guess that the number you unpack has to match the number of things returned rather than being a head and tail.
- OJFord 8y agoNot GP, but it's _very_ limited. Off the top of my head, things you can't do: # nested unpacking (a, b), c = ((1, 2), 3) # destructuring arguments (lambda (a, b): None)((1, 2))
- SirScrambly 8y agoFor your second example, you need to unpack your parameters. (lambda x, y: None)(*(1, 2)) Unless you're saying you should be able to define the set size you pass in.
- OJFord 8y agoYes, it was an example of something you _can't_ do.
- fiddlerwoaroof 8y agoBoth of these were intentionally removed around Python 3, iirc: python 2.7 allows nested tuple/list destructuring as well as destructuring in function arg lists.
- zephyrfalcon 8y agoFirst example works fine in both Python 2 and 3. Second example works in Python 2.7 and earlier, but tuple unpacking for arguments was removed in Python 3. [1] [1] https://www.python.org/dev/peps/pep-3113/ https://www.python.org/dev/peps/pep-3113/
- jolmg 8y agoI feel it's meant to be a part of the features that try to make Python more viable for functional programming, but when you compare it to the equivalent in Haskell, for example: * You can't nest, like it was mentioned ((x1, y1), (x2, y2)) = makeLine ... * You can't use aliases line@(point1@(x1, y1), point2@(x2, y2)) = makeLine ... * You can't use it on all objects/structures/datatypes Line (Point x1 y1) (Point x2 y2) = makeLine ... * You can't pattern match on it case nextShape of Circle centerPoint radius -> ... Square width height -> ... Triangle point1 point2 point3 -> ... * You can't use it on functions/lambdas \ Line (point1@(Point x1 y1) _) -> ... drawLine (Line (point1@(Point x1 y1) _)) = ... * The syntax in Python 2 doesn't support unpacking the first few elements of a list of variable length. Python 3 added support, though. x1:x2:xs = ...
- zephyrfalcon 8y agoTuple/list unpacking has been in Python for a long time and likely has nothing to do with catering to the functional crowd, which Guido isn't known for to begin with. Anyway... > * You can't nest, like it was mentioned ((x1, y1), (x2, y2)) = makeLine ... This works fine in Python: ((x1,y1), (x2,y2)) = ((1,2),(3,4)) I don't know what "makeLine ..." returns, but if it doesn't exactly match the tuples on the left hand side, it won't fly in Haskell either. > * You can't use aliases line@(point1@(x1, y1), point2@(x2, y2)) = makeLine ... No, but this works: line = p1, p2 = ((x1,y1), (x2,y2)) = ((1,2), (3,4)) and is more readable IMHO. Point 3, 4 and 5 don't make sense in Python. This is pattern matching, not tuple/list unpacking. (One could argue that tuple unpacking is a form of pattern matching, but that is a different story...)