Hi Martin,
> On 30 Apr 2022, at 21:35, Martin Mueller <martinmuel...@northwestern.edu>
> wrote:
>
> for elem in elem:
> sort_and_indent(elem, level + 1)
I'd never write the above code — i.e. using the same variable name for the for
loop's iterator as it's input. The ambiguity of what the "elem" variable
refers too after this point feels too dangerous — I don't trust python's
scoping rule to do what you expect.
Personally I would use,
for child_elem in elem:
sort_and_indent(child_elem, level + 1)
As a sanity check on this....
The following:
elem = [1,2,3,4,5]
for e in elem:
print(e)
print(elem)
Gives the following output:
▶ ./elem.py
1
2
3
4
5
[1, 2, 3, 4, 5]
In contrast:
elem = [1,2,3,4,5]
for elem in elem:
print(elem)
print(elem)
Gives:
▶ ./elem.py
1
2
3
4
5
5
That final '5' shows Python's scoping isn't as tight as you hope.
Kind regards,
aid
_______________________________________________
lxml - The Python XML Toolkit mailing list -- lxml@python.org
To unsubscribe send an email to lxml-le...@python.org
https://mail.python.org/mailman3/lists/lxml.python.org/
Member address: arch...@mail-archive.com