List of Dictionaries
Russell E. Owen
owen at xastrox.xwashingtonx.edu.invalid
Fri Mar 8 15:58:20 EST 2002
In article <14dc40e2.0203071559.5e3d4550 at posting.google.com>,
lenny_self at hotmail.com (Lenny Self) wrote:
>I am having a problem I was hoping someone might be able to help me
>with. I am attempting to append several dictionaries to a list for
>further processing, however I am seeing some rather odd behavior.
>
>When I add the first dictionary element to the list all is well. When
>I add the second element to the list both the first and second element
>reference the second dictionary that was added. The behavior
>continues whenever I add an element. So, if I add, say 50
>dictionaries, all of the listes elements reference the last dictionary
>I added....
My guess is that mail.getData() is returning a reference to a dictionary
it uses internally, rather than returning a copy of that dictionary.
Hence you end up with multiple references to the same dictionary.
Usually, whenever I return a list, dictionary or other mutable
application, I return a copy to avoid this problem.(In some situations
this is may be appropriate or impractical, but that usually indicates
poor class design - the contents of an object should usually remain
private).
If you cannot or prefer not to change the application returning the
data, then change the calling program to make a copy of the dicationary
before using it.
-- Russell
--
Return owen
address astro
garbled washington
in header edu
More information about the Python-list
mailing list