python-distilled icon indicating copy to clipboard operation
python-distilled copied to clipboard

2.15 indexing into a potentially empty string

Open wildekat opened this issue 2 years ago • 0 comments

In the comments generator of section 2.15, the way t[0] is used does not seem safe:

comments = (t for t in lines if t[0] == '#') # All comments

t might be empty at that point, so you might consider changing that line to either

comments = (t for t in lines if t and t[0] == '#')

or

comments = (t for t in lines if t.startswith('#'))

One other option is to modify lines so that it never yields empty strings:

- lines = (t.strip() for t in f)
+ lines = (s for t in f if (s := t.strip()))

wildekat avatar Feb 03 '23 10:02 wildekat