Skip to content

bpo-31417 Use the enumerate function where appropriate.#3497

Closed
mbyrnepr2 wants to merge 1 commit into
python:masterfrom
mbyrnepr2:bpo-31417
Closed

bpo-31417 Use the enumerate function where appropriate.#3497
mbyrnepr2 wants to merge 1 commit into
python:masterfrom
mbyrnepr2:bpo-31417

Conversation

@mbyrnepr2
Copy link
Copy Markdown
Contributor

@mbyrnepr2 mbyrnepr2 commented Sep 11, 2017

To make code explicit and more readable

Use the enumerate function to replace occurrences of the pattern:

for i in range(len(sources)): src = sources[i]

https://bugs.python.org/issue31417

https://bugs.python.org/issue31417

@mbyrnepr2 mbyrnepr2 requested a review from a team as a code owner September 11, 2017 10:52
@the-knights-who-say-ni
Copy link
Copy Markdown

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Unfortunately we couldn't find an account corresponding to your GitHub username on bugs.python.org (b.p.o) to verify you have signed the CLA (this might be simply due to a missing "GitHub Name" entry in your b.p.o account settings). This is necessary for legal reasons before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

Thanks again to your contribution and we look forward to looking at it!

Copy link
Copy Markdown
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, except of a minor comment.

Comment thread Lib/modulefinder.py
@@ -554,9 +554,9 @@ def replace_paths_in_code(self, co):
self.processed_paths.append(original_filename)

consts = list(co.co_consts)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we modify this function, I suggest to move type(co) out of the loop:

code_type = type(co)

build = {}
for i in range(len(sources)):
src = sources[i]
for i, src in enumerate(sources):
Copy link
Copy Markdown
Contributor

@MSeifert04 MSeifert04 Sep 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This stell leaves the obj = objects[i], why not just for src, obj in zip(sources, objects):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to stay close to the original code and do what was needed to replace range(len(sources)) with enumerate(sources).
Your suggestion could be done but it looks like the comments in the bug tracker would prefer no changes at all.

Comment thread Lib/inspect.py
specs = []
for i in range(len(args)):
specs.append(convert(args[i]))
for i, arg in enumerate(args):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to keep the i at all here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern can be seen in places in the repository.
For example, there is a similar pattern in the inspect.py module in the Signature class:
for idx, param in enumerate(parameters):
where idx is not subsequently used.

The original code also did not use a standard:
for i in args:

I focused on replacing range(len(args)) with enumerate(args).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants