Naming dictionaries recursively

Paddy paddy3118 at googlemail.com
Sat Aug 18 07:39:32 EDT 2007


On Aug 18, 11:44 am, TYR <a.harrow... at gmail.com> wrote:
> That sounds like a solution. I think the core of the question is as
> follows; if I was to call the dict() function for each line, thus
> creating a dictionary, and then rename it to dict['name'], will there
> be a namespace collision on the next loop when dict() is called again?
> Obviously the first dictionary will still be there, as it still has a
> refcount of 1; but if it gets overwritten, will the object that the
> new name refers to be overwritten too?

if data2name might give colliding names then you could use tuples as
dictionary keys as shown below:

  import modulename
  datadict = dict( ((data2name(d), count), d)
    for count,d in enumerate(modulename.data) )

Generally, if you try and create two keys with the same name in a
dictionary, they will clash and only the second value is retained.

- Paddy.




More information about the Python-list mailing list