Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

but the fact that you can get something sequential out of a dictionary and revert back to tuple assignment is trivial.

I disagree with this assertion. This functionality is not trivial and it wasn't in early Python versions -- certainly not before the introduction of __iter__!

It's existence is testimony to the continuing evolution, and consistency of the Python language. Whether this is a good thing or not I suppose depends on your point of view. I imagine Lispers would see it as a black mark that we had to wait for Guido to implement it. Pythonistas on the other hand probably value the fact that it was well thought out and discussed via a PEP, and is universally implemented. Perhaps both are valid points of view.

Your challenge is interesting because it's so easy to solve in Python that I can't imagine anyone except a Lisp programmer seeing anything in it to object to. In Python it is trivial to replicate the functionality albeit with a different syntax -- and syntax seems to be king for many Lisp fans.

   >>> def unpack(d, keys):
   ...         return (d[key] for key in keys)
   ... 
   >>> d = { 'key1': 'val1', 'key2': 'val2', 'key3': 'val3' }
   >>> 
   >>> (x,y) = unpack(d, ['key1', 'key3'])
   >>> print (x,y)
   ('val1', 'val3')
I like the Python version because I have to understand only two concepts to work out what's happening: Firstly there's a function call. Secondly the iterable result of the call is unpacked to a tuple.

The merits of the Clojure version I'm not qualified to say, but is it really better in all regards than the Python equivalent? Even clarity, readability, comprehensibility?




First of all, I certainly wasn't claiming that you couldn't look up more than one hash key on one line of code, using a function. My point is that you might reasonably want to extend the concept of destructuring further to cover dictionaries, and that macros allow you to extend the language in that way.

Clarity, readability, and comprehensibility are three words that, in programming, mean pretty much the same thing as familiarity. To me, if I weren't looking at the definition of unpack, the above would look weird because I'd think of the Perl function unpack(), which does something very different. unpack(d, keys) above isn't something you'd even write in Clojure because it would be a synonym for (map d keys). Aside from that, it does seem very straightforward, if limited. Clojure does win on generality, because the Clojure version nests, covering sequential data structures at the same time:

   user=> (def d {:a {:a1 [1 2 3] :a2 4} :b 5})
   #'user/d
   user=> (let [{{[_ _ x] :a1} :a, y :b}  d]
             [x y])
   [3 5]
Hashes are the idiomatic general-purpose object in Clojure, so this sort of capability is actually useful in taking apart function arguments.


Ah well that nesting in Clojure is very cool! Python can unpack nested tuples, but my "unpack" function would be very unwieldy there.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: