I found an interesting loop pattern, used at several places in our code. It goes like this:
- I create an empty list
- I start a loop to fill it
- Inside my loop, I create an object and add it to my list
- Still in the loop, I need to modify the object I just added, so I retrieve it from the list
An example here:
templates = []
index = 0
json_templates = json.loads(templates_as_str)
for template_file in json_templates:
templates.add(get_template(template_file))
template = templates[index]
# Do some stuff on my template
index += 1
So yes, I could have created my template reference before storing it in the list. But it's too late! I need to use an index now...
No comments:
Post a Comment