Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RuntimeError: dictionary changed size during iteration

Reply
Thread Tools

RuntimeError: dictionary changed size during iteration

 
 
Robert Dailey
Guest
Posts: n/a
 
      12-08-2008
I'm executing the following code:

def CopyBoost( libraries ):
pass

def CopyEmotionFX( libraries ):
pass


def Copy( library, aliases ):
pass

stuff = vars()
for key in stuff:
print( key, '--', stuff[key] )



I get the following error message:
('CopyEmotionFX', '--', <function CopyEmotionFX at 0x0205BF70>)
Traceback (most recent call last):
File "C:\IT\work\jewett\depends.py", line 12, in <module>
for key in stuff:
RuntimeError: dictionary changed size during iteration

Why is this happening?
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      12-09-2008
Robert Dailey wrote:

> stuff = vars()


>>> vars() is globals()

True

> for key in stuff:


You just changed globals, which is aliased as stuff.
Stuff changes.

> print( key, '--', stuff[key] )
>
>
>
> I get the following error message:
> ('CopyEmotionFX', '--', <function CopyEmotionFX at 0x0205BF70>)
> Traceback (most recent call last):
> File "C:\IT\work\jewett\depends.py", line 12, in <module>
> for key in stuff:
> RuntimeError: dictionary changed size during iteration
>
> Why is this happening?


 
Reply With Quote
 
 
 
 
Robert Dailey
Guest
Posts: n/a
 
      12-09-2008
On Dec 8, 6:26*pm, Terry Reedy <(E-Mail Removed)> wrote:
> Robert Dailey wrote:
> > stuff = vars()

>
> *>>> vars() is globals()
> True
>
> > for key in stuff:

>
> You just changed globals, which is aliased as stuff.
> Stuff changes.
>
> > * * print( key, '--', stuff[key] )

>
> > I get the following error message:
> > ('CopyEmotionFX', '--', <function CopyEmotionFX at 0x0205BF70>)
> > Traceback (most recent call last):
> > * File "C:\IT\work\jewett\depends.py", line 12, in <module>
> > * * for key in stuff:
> > RuntimeError: dictionary changed size during iteration

>
> > Why is this happening?

>
>


How am I changing globals()? I'm simply iterating the keys in the
dict. Can someone explain what is going on please?
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      12-09-2008
On Mon, 08 Dec 2008 19:10:00 -0800, Robert Dailey wrote:

> On Dec 8, 6:26*pm, Terry Reedy <(E-Mail Removed)> wrote:
>> Robert Dailey wrote:
>> > stuff = vars()

>>
>> *>>> vars() is globals()
>> True
>>
>> > for key in stuff:

>>
>> You just changed globals, which is aliased as stuff. Stuff changes.
>>
>> > * * print( key, '--', stuff[key] )

>>
>> > I get the following error message:
>> > ('CopyEmotionFX', '--', <function CopyEmotionFX at 0x0205BF70>)
>> > Traceback (most recent call last):
>> > * File "C:\IT\work\jewett\depends.py", line 12, in <module>
>> > * * for key in stuff:
>> > RuntimeError: dictionary changed size during iteration

>>
>> > Why is this happening?

>>
>>
>>

> How am I changing globals()? I'm simply iterating the keys in the dict.
> Can someone explain what is going on please?


You create an new name "key":

for key in stuff

I suppose you could do this:


key = None
stuff = vars()
for key in stuff:


but even better would be:

for key in vars().copy():

because that protects you from cases where globals() change inside the
for loop.



--
Steven
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      12-09-2008
On Dec 9, 3:00*pm, Steven D'Aprano
<(E-Mail Removed)> wrote:
> On Mon, 08 Dec 2008 19:10:00 -0800, Robert Dailey wrote:
> > On Dec 8, 6:26*pm, Terry Reedy <(E-Mail Removed)> wrote:
> >> Robert Dailey wrote:
> >> > stuff = vars()

>
> >> *>>> vars() is globals()
> >> True

>
> >> > for key in stuff:

>
> >> You just changed globals, which is aliased as stuff. Stuff changes.

>
> >> > * * print( key, '--', stuff[key] )

>
> >> > I get the following error message:
> >> > ('CopyEmotionFX', '--', <function CopyEmotionFX at 0x0205BF70>)
> >> > Traceback (most recent call last):
> >> > * File "C:\IT\work\jewett\depends.py", line 12, in <module>
> >> > * * for key in stuff:
> >> > RuntimeError: dictionary changed size during iteration

>
> >> > Why is this happening?

>
> > How am I changing globals()? I'm simply iterating the keys in the dict.
> > Can someone explain what is going on please?

>
> You create an new name "key":
>
> for key in stuff
>
> I suppose you could do this:
>
> key = None
> stuff = vars()
> for key in stuff:


but both 'key' and 'stuff' will appear in the dict, possibly causing
confusion; another reason why

> even better would be:
>
> for key in vars().copy():
>
> because that protects you from cases where globals() change inside the
> for loop.

 
Reply With Quote
 
Robert Dailey
Guest
Posts: n/a
 
      12-09-2008
On Dec 8, 10:27*pm, John Machin <(E-Mail Removed)> wrote:
> On Dec 9, 3:00*pm, Steven D'Aprano
>
>
>
> <(E-Mail Removed)> wrote:
> > On Mon, 08 Dec 2008 19:10:00 -0800, Robert Dailey wrote:
> > > On Dec 8, 6:26*pm, Terry Reedy <(E-Mail Removed)> wrote:
> > >> Robert Dailey wrote:
> > >> > stuff = vars()

>
> > >> *>>> vars() is globals()
> > >> True

>
> > >> > for key in stuff:

>
> > >> You just changed globals, which is aliased as stuff. Stuff changes.

>
> > >> > * * print( key, '--', stuff[key] )

>
> > >> > I get the following error message:
> > >> > ('CopyEmotionFX', '--', <function CopyEmotionFX at 0x0205BF70>)
> > >> > Traceback (most recent call last):
> > >> > * File "C:\IT\work\jewett\depends.py", line 12, in <module>
> > >> > * * for key in stuff:
> > >> > RuntimeError: dictionary changed size during iteration

>
> > >> > Why is this happening?

>
> > > How am I changing globals()? I'm simply iterating the keys in the dict.
> > > Can someone explain what is going on please?

>
> > You create an new name "key":

>
> > for key in stuff

>
> > I suppose you could do this:

>
> > key = None
> > stuff = vars()
> > for key in stuff:

>
> but both 'key' and 'stuff' will appear in the dict, possibly causing
> confusion; another reason why
>
> > even better would be:

>
> > for key in vars().copy():

>
> > because that protects you from cases where globals() change inside the
> > for loop.

>
>


When I do:

for key in stuff.keys():


It works! I wonder why .keys() makes a difference. It is using a
'view', which is a new concept in Python 3.0 that I'm not totally
familiar with yet.
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      12-09-2008
Robert Dailey wrote:

> When I do:
>
> for key in stuff.keys():
>
>
> It works! I wonder why .keys() makes a difference. It is using a
> 'view', which is a new concept in Python 3.0 that I'm not totally
> familiar with yet.


Because stuff.keys() is evaluated *once* and the result is a separate
object from stuff == globals(), so creating the new entry 'key' in
globals == stuff does not change that new object.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dictionary size changed during iteration Laszlo Nagy Python 3 04-22-2011 06:22 AM
"RuntimeError: dictionary changed size during iteration" ; Good atomiccopy operations? robert Python 29 03-14-2006 06:22 AM
Re: "RuntimeError: dictionary changed size during iteration" ;Good atomic copy operations? Jean-Paul Calderone Python 0 03-13-2006 03:41 AM
Re: RuntimeError: dictionary changed size during iteration Terry Reedy Python 0 01-20-2005 06:40 PM
RuntimeError: dictionary changed size during iteration Roman Suzi Python 0 01-19-2005 08:45 PM



Advertisments