<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'><id>tag:blogger.com,1999:blog-5440028356946346379.post1746139113230710340..comments</id><updated>2008-03-25T19:06:43.590-04:00</updated><title type='text'>Comments on Doug Hellmann: PyMOTW: collections</title><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://blog.doughellmann.com/feeds/1746139113230710340/comments/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5440028356946346379/1746139113230710340/comments/default'/><link rel='alternate' type='text/html' href='http://blog.doughellmann.com/2008/03/pymotw-collections.html'/><author><name>Doug Hellmann</name><uri>http://www.blogger.com/profile/01892352754222143463</uri><email>noreply@blogger.com</email></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>4</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5440028356946346379.post-3005968073698636401</id><published>2008-03-25T19:06:43.590-04:00</published><updated>2008-03-25T19:06:43.590-04:00</updated><title type='text'>@Ido - I hadn't heard of Peter's DefaultDict befor...</title><content type='html'>@Ido - I hadn't heard of Peter's DefaultDict before.  It looks like the semantics are the same as the version in the standard library.  Thanks for the pointer.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5440028356946346379/1746139113230710340/comments/default/3005968073698636401'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5440028356946346379/1746139113230710340/comments/default/3005968073698636401'/><link rel='alternate' type='text/html' href='http://blog.doughellmann.com/2008/03/pymotw-collections.html?showComment=1206486403590#c3005968073698636401' title=''/><author><name>Doug Hellmann</name><uri>http://www.blogger.com/profile/01892352754222143463</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00116818175230541568'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.doughellmann.com/2008/03/pymotw-collections.html' ref='tag:blogger.com,1999:blog-5440028356946346379.post-1746139113230710340' source='http://www.blogger.com/feeds/5440028356946346379/posts/default/1746139113230710340' type='text/html'/></entry><entry><id>tag:blogger.com,1999:blog-5440028356946346379.post-7721727847063722050</id><published>2008-03-25T09:09:07.898-04:00</published><updated>2008-03-25T09:09:07.898-04:00</updated><title type='text'>I didn't know about collections.defaultdict, nice ...</title><content type='html'>I didn't know about collections.defaultdict, nice :)&lt;BR/&gt;&lt;BR/&gt;until now, I've used &lt;A HREF="http://norvig.com/python-iaq.html" REL="nofollow"&gt;peter norvig's&lt;/A&gt; DefaultDict:&lt;BR/&gt;&lt;BR/&gt;class DefaultDict(dict):&lt;BR/&gt;    """Dictionary with a default value for unknown keys."""&lt;BR/&gt;    def __init__(self, default):&lt;BR/&gt;        self.default = default&lt;BR/&gt;&lt;BR/&gt;    def __getitem__(self, key):&lt;BR/&gt;        if key in self: return self.get(key)&lt;BR/&gt;        return self.setdefault(key, copy.deepcopy(self.default))&lt;BR/&gt;&lt;BR/&gt;&gt;&gt;&gt; d = DefaultDict([])&lt;BR/&gt;&gt;&gt;&gt; d['foo'].append('bar')&lt;BR/&gt;&gt;&gt;&gt; d&lt;BR/&gt;{'foo': ['bar']}</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5440028356946346379/1746139113230710340/comments/default/7721727847063722050'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5440028356946346379/1746139113230710340/comments/default/7721727847063722050'/><link rel='alternate' type='text/html' href='http://blog.doughellmann.com/2008/03/pymotw-collections.html?showComment=1206450547898#c7721727847063722050' title=''/><author><name>Ido</name><email>noreply@blogger.com</email></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.doughellmann.com/2008/03/pymotw-collections.html' ref='tag:blogger.com,1999:blog-5440028356946346379.post-1746139113230710340' source='http://www.blogger.com/feeds/5440028356946346379/posts/default/1746139113230710340' type='text/html'/></entry><entry><id>tag:blogger.com,1999:blog-5440028356946346379.post-8852754409137591439</id><published>2008-03-24T09:34:19.818-04:00</published><updated>2008-03-24T09:34:19.818-04:00</updated><title type='text'>Hi, Michael,Yes, I agree that the defaultdict API ...</title><content type='html'>Hi, Michael,&lt;BR/&gt;&lt;BR/&gt;Yes, I agree that the defaultdict API is easier to follow.  It's also more convenient when passing the dictionary off to another part of your code where key names might be assumed, or required, but not present.&lt;BR/&gt;&lt;BR/&gt;I like the idea of having setdefaultfactory() on a standard dictionary, too.  Have you written a PEP for that?&lt;BR/&gt;&lt;BR/&gt;Doug</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5440028356946346379/1746139113230710340/comments/default/8852754409137591439'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5440028356946346379/1746139113230710340/comments/default/8852754409137591439'/><link rel='alternate' type='text/html' href='http://blog.doughellmann.com/2008/03/pymotw-collections.html?showComment=1206365659818#c8852754409137591439' title=''/><author><name>Doug Hellmann</name><uri>http://www.blogger.com/profile/01892352754222143463</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00116818175230541568'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.doughellmann.com/2008/03/pymotw-collections.html' ref='tag:blogger.com,1999:blog-5440028356946346379.post-1746139113230710340' source='http://www.blogger.com/feeds/5440028356946346379/posts/default/1746139113230710340' type='text/html'/></entry><entry><id>tag:blogger.com,1999:blog-5440028356946346379.post-712315347850870869</id><published>2008-03-23T12:38:54.022-04:00</published><updated>2008-03-23T12:38:54.022-04:00</updated><title type='text'>The setdefault method of dict is used in a complet...</title><content type='html'>The setdefault method of dict is used in a completely different scenario than collections.defaultdict. &lt;BR/&gt;&lt;BR/&gt;dict.setdefault is used when you want to set a certain member of the dictionary when a key is chosen that doesn't yet have a mapping, while also returning it. This key is specified when you are setting the item, and can be different for different accesses of the dict, as the dict does not store the factory method. It also has to be the most confusingly named function in the base python API, almost as bad as the inconsistent read/write signatures for ConfigParser, and for that reason alone, should be considered worth avoiding.&lt;BR/&gt;&lt;BR/&gt;collections.defaultdict is used when you want to create an entirely new member for each unknown key, and you don't want to have to specify (and sometimes respecify) each default with every call. This is really a "dict.get call with an automatic set"&lt;BR/&gt;&lt;BR/&gt;IMO, the collections.defaultdict should be preferred, as it is much clearer and works semantically how people expect. The former is highly non-intuitive, always creates the default object, even if unneeded, and is more complicated to use, therefore more error prone.&lt;BR/&gt;&lt;BR/&gt;     --Michael&lt;BR/&gt;&lt;BR/&gt;PS: IMO, collections.defaultdict should be moved to the standard dict as setdefaultfactory and the use of the confusing dict.setdefault should be deprecated.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5440028356946346379/1746139113230710340/comments/default/712315347850870869'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5440028356946346379/1746139113230710340/comments/default/712315347850870869'/><link rel='alternate' type='text/html' href='http://blog.doughellmann.com/2008/03/pymotw-collections.html?showComment=1206290334022#c712315347850870869' title=''/><author><name>Michael Langford</name><uri>http://www.blogger.com/profile/13829568026331381254</uri><email>noreply@blogger.com</email></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.doughellmann.com/2008/03/pymotw-collections.html' ref='tag:blogger.com,1999:blog-5440028356946346379.post-1746139113230710340' source='http://www.blogger.com/feeds/5440028356946346379/posts/default/1746139113230710340' type='text/html'/></entry></feed>