Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: Add small clarification on Chapter 22, Example 22-5. explore1.py #48

Open
andyLaurito92 opened this issue Oct 31, 2024 · 0 comments

Comments

@andyLaurito92
Copy link

Hi!

I was going through script explore1.py from chapter 22, in particular in the init method and I wrote the following code instead of the code in the script:

self.__data = dict(mapping)
for key, val in self.__data.items():
    if keyword.iskeyword(key):
        newkey = key + '_'
        self[newkey] = val
        self.__data.pop(key)

Of course the above code raised the following exception:

RuntimeError: dictionary keys changed during iteration

Which googling a bit lead me to the conclusion that modifying containers while looping is something that never should be done and I should use a shallow copy instead if necessary :)

I'm creating this issue as a suggestion for perhaps a small warning/caution for a future edition. Doing a quick search in stackoverflow of the above issue showed me that this is something recurrent, even Alex Martelli did a comment about this 14 years ago here. Perhaps we can avoid some future Python programmers to stumble across this issue by warning them! 😃

Btw, thank you very much for the outstanding book! Looking forward for that next edition 😄

Best,

Andy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant