This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rhettinger
Recipients dohertywa, rhettinger
Date 2007-10-16.22:35:46
SpamBayes Score 0.09803078
Marked as misclassified No
Message-id <1192574147.66.0.461025822352.issue1288@psf.upfronthosting.co.za>
In-reply-to
Content
This isn't a bug.  Writing
"dict.fromkeys(xrange(1,48),dict.fromkeys(xrange(1,8),0))" results in
the inner expression being evaluated just once and then passed to the
outer function call as a fully evaluated argument.  As a result, the
*same* dictionary is being used over and over again.

People commonly encounter similar issue when they try to create
initialized list-of-lists with something like s=[[0]*10] which repeats
ten of the *same* lists.  Instead they should write something like: 
s=[[0] for i in range(10)] which creates *distinct* inner lists.

For you application, consider using a defaultdict which can call a
function as needed to create new, distinct values:

  d = defaultdict(lambda: dict.fromkeys(xrange(1,8), 0))
History
Date User Action Args
2007-10-16 22:35:47rhettingersetspambayes_score: 0.0980308 -> 0.09803078
recipients: + rhettinger, dohertywa
2007-10-16 22:35:47rhettingersetspambayes_score: 0.0980308 -> 0.0980308
messageid: <1192574147.66.0.461025822352.issue1288@psf.upfronthosting.co.za>
2007-10-16 22:35:47rhettingerlinkissue1288 messages
2007-10-16 22:35:46rhettingercreate