<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>t+1 - Latest Comments in defaultdict.fromkeys does not play nice.</title><link>http://tplus1.disqus.com/</link><description></description><atom:link href="http://tplus1.disqus.com/defaultdictfromkeys_does_not_play_nice/latest.rss" rel="self"></atom:link><language>en</language><lastBuildDate>Wed, 04 Jun 2008 10:32:46 -0000</lastBuildDate><item><title>Re: defaultdict.fromkeys does not play nice.</title><link>http://blog.tplus1.com/index.php/2008/06/03/defaultdictfromkeys-does-not-play-nice/#comment-589310</link><description>&lt;p&gt;Hey Georg,&lt;/p&gt;

&lt;p&gt;Yeah, I agree that the defaultdict.fromkeys behavior is not a bug, but it does seem a little surprising.  Anyhow, I posted to comp.lang.python &lt;a href="http://groups.google.com/group/comp.lang.python/browse_thread/thread/9a93f02c7ecd018a#" rel="nofollow"&gt;here&lt;/a&gt; about this topic and got some pretty good explanation.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Matt Wilson</dc:creator><pubDate>Wed, 04 Jun 2008 10:32:46 -0000</pubDate></item><item><title>Re: defaultdict.fromkeys does not play nice.</title><link>http://blog.tplus1.com/index.php/2008/06/03/defaultdictfromkeys-does-not-play-nice/#comment-587498</link><description>&lt;p&gt;With dictionaries, the second argument for fromkeys() is the value (not a value factory) that all keys from the first argument will be set to. Since defaultdict.fromkeys() has no special handling, this works the same with defaultdict. That's not a bug to me.&lt;/p&gt;

&lt;p&gt;Of course defaultdict.fromkeys could be extended to take a factory function argument, but as you have seen you can set the factory yourself too.&lt;/p&gt;

&lt;p&gt;All in all, I don't see the value of using fromkeys() for a defaultdict.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Georg Brandl</dc:creator><pubDate>Wed, 04 Jun 2008 03:14:29 -0000</pubDate></item><item><title>Re: defaultdict.fromkeys does not play nice.</title><link>http://blog.tplus1.com/index.php/2008/06/03/defaultdictfromkeys-does-not-play-nice/#comment-581190</link><description>&lt;p&gt;Ahh... good point.  Clearly I didn't think it through. :)&lt;/p&gt;

&lt;p&gt;I considered taking a stab at fixing this, but the effort involved for a first-time Python contributor is prohibitive.  So laziness wins again.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Gary Bernhardt</dc:creator><pubDate>Tue, 03 Jun 2008 14:42:49 -0000</pubDate></item><item><title>Re: defaultdict.fromkeys does not play nice.</title><link>http://blog.tplus1.com/index.php/2008/06/03/defaultdictfromkeys-does-not-play-nice/#comment-580581</link><description>&lt;p&gt;Hey Gary, thanks for the comment.&lt;/p&gt;

&lt;p&gt;Setting the default_factory after I make an instance doesn't go back and rerun the keys through the default factory.  So, in that sense, fromkeys is kinda useless to me.  That's why I'd prefer that it just raised that NotImplementedError.&lt;/p&gt;

&lt;p&gt;&amp;gt;&amp;gt;&amp;gt; b = defaultdict.fromkeys(['x', 'y'], list)&lt;br&gt;&amp;gt;&amp;gt;&amp;gt; b&lt;br&gt;defaultdict(None, {'y': &amp;lt;type 'list'=""&amp;gt;, 'x': &amp;lt;type 'list'=""&amp;gt;})&lt;br&gt;&amp;gt;&amp;gt;&amp;gt; b.default_factory = list&lt;br&gt;&amp;gt;&amp;gt;&amp;gt; b['z']&lt;br&gt;[]&lt;br&gt;&amp;gt;&amp;gt;&amp;gt; b['x']&lt;br&gt;&amp;lt;type 'list'=""&amp;gt;&amp;lt;/type&amp;gt;&amp;lt;/type&amp;gt;&amp;lt;/type&amp;gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Matt Wilson</dc:creator><pubDate>Tue, 03 Jun 2008 13:33:59 -0000</pubDate></item><item><title>Re: defaultdict.fromkeys does not play nice.</title><link>http://blog.tplus1.com/index.php/2008/06/03/defaultdictfromkeys-does-not-play-nice/#comment-580476</link><description>&lt;p&gt;It looks like defaultdict.fromkeys just fails to pass the factory function along:&lt;/p&gt;

&lt;p&gt;&amp;gt;&amp;gt;&amp;gt; defaultdict(list)&lt;br&gt;defaultdict(&amp;lt;type 'list'=""&amp;gt;, {})&lt;br&gt;&amp;gt;&amp;gt;&amp;gt; defaultdict.fromkeys([], list)&lt;br&gt;defaultdict(None, {})&lt;/p&gt;

&lt;p&gt;There's no mention of fromkeys in the collections module's source, so it's probably just inheriting it from dict.  That makes sense - dict doesn't know about the default_factory.  You can manually set the defaultdict's default_factory argument to fix it.&amp;lt;/type&amp;gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Gary Bernhardt</dc:creator><pubDate>Tue, 03 Jun 2008 13:19:31 -0000</pubDate></item></channel></rss>
