Hacker News new | past | comments | ask | show | jobs | submit login
Python IAQ: Infrequently Answered Questions (norvig.com)
50 points by dpritchett on April 21, 2010 | hide | past | favorite | 8 comments



It's cool how many of these kludges are fixed in more recent Pythons, especially Python 3 (and its features backported to 2.6 and 2.7):

Q3. Polymorphism, and the lack of sequence and number types -- there are now abstract base classes for Iterable and Number types, etc.

Q6. printf function -- print() is indeed a function in Python 3

Q8. object syntax shortcut -- collections.namedtuple in Py2.6 is similar, acting something like a factory for anonymous classes

Q9. syntax for updating objects -- in recent Pythons, this works: obj.__dict__.update(foo=1, bar=2)

Q10. dictionary with default values -- collections.defaultdict

Q13. abstract base classes -- hilarious, but now there's a decorator in the abc module that does the right thing

Q18. missing queue types -- collections.deque is a good double-ended queue, and the heapq module can turn a list into a heap queue.

You can also see how many of his other wish-list items came true, especially via the itertools module, in his personal 'utils' module:

http://aima.cs.berkeley.edu/python/utils.html


You can also say a_dict.setdefault(key, default) which is like a_dict.get(key, default) but will also do a_dict[key]=default if there does not already exist a value for key.


A funny little bit of snark hidden in the middle, following some decidedly unpythonic code:

If u cn rd ths, u cn gt a jb in fncnl prg (if thr wr any).


The code preceding that is actually pretty simple if you translate it to Haskell:

    my_if test result alternative = if test then result else alternative
It's just trying to get currying and lazy evaluation in Python which causes all the ugliness.


Unfortunately it's quite old, and some of the answers are rather out of date.


It appears you're right! I can't find a date on Norvig's site but here's a link to it from 2001: http://www.brokenrobot.org/archives/000427.html


Norvig did some curation over the years, however. One of the answers mentions that the ternary "if" expression is scheduled to appear in Python 2.5, which is information from around 2006.


Indeed, defaultdict was added in 2.5 to the collections module




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

Search: