on the popularity of loops while and for

Chris Angelico rosuav at gmail.com
Sat Aug 28 17:43:35 EDT 2021


On Sun, Aug 29, 2021 at 7:40 AM Hope Rouselle <hrouselle at jevedi.com> wrote:
>
> I'd like get a statistic of how often each loop is used in practice.
>
> I was trying to take a look at the Python's standard libraries --- those
> included in a standard installation of Python 3.9.6, say --- to see
> which loops are more often used among while and for loops.  Of course,
> since English use the preposition ``for'' a lot, that makes my life
> harder.  Removing comments is easy, but removing strings is harder.  So
> I don't know yet what I'll do.
>
> Have you guys ever measured something like that in a casual or serious
> way?  I'd love to know.  Thank you!

For analysis like this, I recommend using the Abstract Syntax Tree:

https://docs.python.org/3/library/ast.html

You can take a Python source file, parse it to the AST, and then walk
that tree to see what it's using. That will avoid any false positives
from the word "for" coming up in the wrong places.

ChrisA


More information about the Python-list mailing list