Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
@andyLaurito92

Description

@andyLaurito92

Hi!

I was going through scriptexplore1.py from chapter 22, in particular in theinit 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 aquick search in stackoverflow of the above issue showed me that this is something recurrent, even Alex Martelli did a comment about this 14 years agohere. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp