Do not use for i in range(len(<array>))
This product is not supported for your selected
Datadog site. (
).
이 페이지는 아직 영어로 제공되지 않습니다. 번역 작업 중입니다.
현재 번역 프로젝트에 대한 질문이나 피드백이 있으신 경우
언제든지 연락주시기 바랍니다.ID: python-best-practices/no-range-loop-with-len
Language: Python
Severity: Error
Category: Best Practices
Description
Do not iterate over an array using for in range(len(array)). Use instead for i in array.
Learn More
Non-Compliant Code Examples
import random
import string
DEFAULT_CHAR_STRING = string.ascii_lowercase + string.digits
def generate_random_string(chars=DEFAULT_CHAR_STRING, size=20):
return "".join(random.choice(chars) for _ in range(size))
NORMALIZED_SIZE = 8
def normalize(s):
builder = []
for i in range(NORMALIZED_SIZE):
builder.append(s[i].capitalize())
return "".join(builder)
FORBIDDEN_WORDS = set(["bad1", "bad2"])
def filter_forbidden_tags(tags):
for i in range(len(tags)):
if tags[i].tag in FORBIDDEN_WORDS:
del tags[i]
return tags
for i in range(len(tab)): # iterate directly over the array
bla(tab[i])
for i in range(len(tab)): # iterate directly over the array
bla(tab[i])
for i in range(len(tab)): # iterate directly over the array
tab[i] = bla
Compliant Code Examples
for i, _ in enumerate(lst):
print(i)
for i in range(len(lst)):
print(i)
plop = tab[i]
원활한 통합. Datadog Code Security를 경험해 보세요