Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   RuntimeError: dictionary changed size during iteration (http://www.velocityreviews.com/forums/t647819-runtimeerror-dictionary-changed-size-during-iteration.html)

Robert Dailey 12-08-2008 10:56 PM

RuntimeError: dictionary changed size during iteration
 
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?

Terry Reedy 12-09-2008 12:26 AM

Re: RuntimeError: dictionary changed size during iteration
 
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?



Robert Dailey 12-09-2008 03:10 AM

Re: RuntimeError: dictionary changed size during iteration
 
On Dec 8, 6:26*pm, Terry Reedy <tjre...@udel.edu> 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?

Steven D'Aprano 12-09-2008 04:00 AM

Re: RuntimeError: dictionary changed size during iteration
 
On Mon, 08 Dec 2008 19:10:00 -0800, Robert Dailey wrote:

> On Dec 8, 6:26*pm, Terry Reedy <tjre...@udel.edu> 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

John Machin 12-09-2008 04:27 AM

Re: RuntimeError: dictionary changed size during iteration
 
On Dec 9, 3:00*pm, Steven D'Aprano
<ste...@REMOVE.THIS.cybersource.com.au> wrote:
> On Mon, 08 Dec 2008 19:10:00 -0800, Robert Dailey wrote:
> > On Dec 8, 6:26*pm, Terry Reedy <tjre...@udel.edu> 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.


Robert Dailey 12-09-2008 03:20 PM

Re: RuntimeError: dictionary changed size during iteration
 
On Dec 8, 10:27*pm, John Machin <sjmac...@lexicon.net> wrote:
> On Dec 9, 3:00*pm, Steven D'Aprano
>
>
>
> <ste...@REMOVE.THIS.cybersource.com.au> wrote:
> > On Mon, 08 Dec 2008 19:10:00 -0800, Robert Dailey wrote:
> > > On Dec 8, 6:26*pm, Terry Reedy <tjre...@udel.edu> 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.

Terry Reedy 12-09-2008 06:25 PM

Re: RuntimeError: dictionary changed size during iteration
 
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.



All times are GMT. The time now is 01:57 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.