Re: A question about readability

Discussion in 'Python' started by rusi, Dec 10, 2012.

  1. rusi

    rusi Guest

    On Dec 10, 3:03 pm, Jean-Michel Pichavant <>
    wrote:
    > ----- Original Message -----
    > > On Dec 7, 6:46 pm, Marco <> 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 sums the elements of the lines of this file:

    >
    > > > $ more data.txt
    > > > 30;44;99;88
    > > > 11;17;16;50
    > > > 33;91;77;15
    > > > $ python3.3 myscript.py
    > > > 261
    > > > 94
    > > > 216

    >
    > > > is explicit enough? Do you prefer a clearer solution?
    > > > Thanks in advance, Marco
    > > > --
    > > > Marco

    >
    > > Interpreting your question as a general question of stylistics, my
    > > experience is that a 3 line script often becomes a 10 line or a 50
    > > line script at which point the direct printing will have to be
    > > modified to create an internal data structure.

    >
    > > So on the whole I find it expedient to start with that assumption and
    > > write it as:

    >
    > > def linesums(file):
    > >   return [sum(int(i) for i in l.split(';')) for l in open(file, 'r')]

    >
    > Why change the OP's namings ? 'data' and 'line' were more suitable than 'i' and 'l'. Of course we're nitpicking, no one will get hurt.
    >
    > JM


    Yes, l is an undesirable name because it can look like 1
     
    rusi, Dec 10, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Stockwell
    Replies:
    3
    Views:
    315
    Peter Hansen
    Jun 2, 2004
  2. Replies:
    9
    Views:
    346
    Jerry Coffin
    Jul 28, 2006
  3. DE
    Replies:
    16
    Views:
    823
  4. Replies:
    3
    Views:
    241
    Nick Keighley
    Mar 17, 2010
  5. Marco

    A question about readability

    Marco, Dec 7, 2012, in forum: Python
    Replies:
    3
    Views:
    162
Loading...

Share This Page