4 ms·
Python IAQ: Infrequently Answered Questions
- kingkilr 17y agoUnfortunately it's quite old, and some of the answers are rather out of date.
- dpritchett 17y agoIt 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 http://www.brokenrobot.org/archives/000427.html
- jcl 17y agoNorvig 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.
- mumrah 17y agoIndeed, defaultdict was added in 2.5 to the collections module
- jcl 17y agoA 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).
- sketerpot 17y agoThe 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.
- etal 17y agoIt'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 http://aima.cs.berkeley.edu/python/utils.html
- koenigdavidmj 17y agoYou 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.