A question about readability

Roy Smith roy at panix.com
Fri Dec 7 08:57:54 EST 2012


In article <k9srup$kc7$1 at speranza.aioe.org>,
 Marco <name.surname at gmail.com> wrote:

> Hi all, do you think this code:
> 
> $ more myscript.py
> for line in open('data.txt'):
>      result = sum(int(data) for data in line.split(';'))
>      print(result)

That sum() line is a bit of a mouthful.  I would refactor it into 
something like this:

> for line in open('data.txt'):
>      fields = line.split(';')
>      print sum(int(i) for i in fields)

It's the same number of lines, but I think it's easier to read.

BTW, are you using Python 2 or 3?  It helps when asking these kinds of 
questions to let people know.  I assumed 2 in my answer above.



More information about the Python-list mailing list