RFC: For Loop Invariants

Peter J. Holzer hjp-python at hjp.at
Sat Apr 11 17:53:48 EDT 2020


On 2020-04-10 15:44:05 -0500, Elliott Dehnbostel wrote:
> *Consider the following trivial for-loop:*
> 
> chars = "abcaaabkjzhbjacvb"
> seek = {'a','b','c'}
> count = 0
> for a in chars:
>      if a in seek:
>           count += 1
> 
> Gross. Twice nested for a simple count.
> 
[...]
> I propose the following enhancement:
> 
> chars = "abcaaabkjzhbjacvb"
> seek = {'a','b','c'}
> count = 0
> for a in chars if a in seek: count += 1

I don't see much of an enhancement here. You save writing one colon and
one newline here. The code itself is exactly the same. If the original
was gross (I don't agree), then this is just as gross.

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp at hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20200411/ae407dfb/attachment.sig>


More information about the Python-list mailing list