[issue10533] Need example of using __missing__
Raymond Hettinger rhettin...@users.sourceforge.net added the comment: See r87858. -- resolution: - fixed status: open - closed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue10533 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue10533] Need example of using __missing__
Éric Araujo mer...@netwok.org added the comment: Great, thanks. We’ll see later if more cross-links are needed. -- stage: needs patch - committed/rejected ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue10533 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue10533] Need example of using __missing__
Éric Araujo mer...@netwok.org added the comment: 4. I cannot come up with another typical integer value that would be useful, then again I've used , [] and set() numerous times. You can get '' with str as default factory and [] with list. I think we agree on reclassifying this as a doc problem. Proposal: 1) Add a small example in stdtypes.rst:dict section. 2) Add examples of giving dict or int to collections.defaultdict to get {} or 0 as default value. 3) Find a way to link to that example from the index. It is not currently indexed because there is no dict.__missing__ method, hence no method markup that would trigger indexing. 4) Cross-link collections.rst:defaultdict, collections.rst:Counter and stdtypes.rst:__missing__ example. -- components: +Documentation -Library (Lib) nosy: +d...@python stage: - needs patch title: defaultdict constructor with a concrete value - Need example of using __missing__ versions: +Python 2.7, Python 3.1 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue10533 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue10533] Need example of using __missing__
Michael Foord mich...@voidspace.org.uk added the comment: Well, I was perfectly aware of __missing__ - it's just a three liner to do it when using a lambda isn't *that* bad... I'm sure the documentation could be improved to highlight __missing__ though. It's almost always the case that documentation can be improved. :-) -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue10533 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue10533] Need example of using __missing__
Raymond Hettinger rhettin...@users.sourceforge.net added the comment: [Łukasz Langa] __missing__ didn't appear to be the one obvious way to anyone. Two thoughts: * There is part of the Zen that says that way may not be obvious unless your Dutch. In this case, __missing__ was the API designed by Guido to handle the problem. If it isn't obvious, it is up to us to popularize the idiom in talks, in tutorials, in newsgroup discussions, etc. And FWIW, it is not unprecedented -- Lua's dicts have a similar hook and most of the interesting programming in Lua relies on it. * The __missing__ method should not be considered a dark corner of Python. Unlike the copy module, pickling, of weakrefs, it is not tucked away in the library. The missing method is part of the basic API for one of Python's most fundamental objects. If someone is going to be a Python programmer, they must at least learn about dicts, lists, strings, and tuples. then again I've used , [] and set() numerous times. Adding zerodict, stringdict, listdict, setdict is obviously absurd. Looking at those examples, you're probably already aware that the list and set versions are already served by defaultdict(), and that it would be basic (and probably common) mistake to accidentally use [] in your proposed constant dict. A constant version of the defaultdict only makes sense with immutables such as numbers, strings, and tuples. The common case would be the number zero and we have Counter() for that. So, you're left with very few use cases and with a hazard for users who may write: f = fallback_dict([]). 4. I cannot come up with another typical integer value that would be useful FWIW, the Counter class *is* a ZeroDict. It has a few extra methods but is basically a dict with __missing__ set to return zero. ... then I'm +1 on correcting the docs in terms of __missing__ and leaving the implementation as is. Thank you. Will reclassify this as a doc issue. [Éric Araujo] 2) Add examples of giving dict or int to collections.defaultdict to get {} or 0 as default value. Those examples have been there since day one. [Michael Foord] I'm sure the documentation could be improved to highlight __missing__ though. I'll add another example and perhaps include on in the tutorial. It's almost always the case that documentation can be improved. :-) Getting people to read it is another story ;-) -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue10533 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com