Coding style

Discussion in 'Python' started by PTY, Jul 17, 2006.

  1. PTY

    PTY Guest

    Which is better?

    lst = [1,2,3,4,5]

    while lst:
    lst.pop()

    OR

    while len(lst) > 0:
    lst.pop()
    PTY, Jul 17, 2006
    #1
    1. Advertising

  2. PTY enlightened us with:
    > Which is better?
    >
    > lst = [1,2,3,4,5]
    >
    > while lst:
    > lst.pop()
    >
    > OR
    >
    > while len(lst) > 0:
    > lst.pop()


    The first one.

    Sybren
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
    Frank Zappa
    Sybren Stuvel, Jul 17, 2006
    #2
    1. Advertising

  3. On 17 Jul 2006 08:56:34 -0700, PTY <> wrote:
    > Which is better?
    >
    > lst = [1,2,3,4,5]
    >
    > while lst:
    > lst.pop()
    >
    > OR
    >
    > while len(lst) > 0:
    > lst.pop()


    How about:

    lst = [1,2,3,4,5]
    while lst:
    lst.pop()

    Or even just:

    lst = []

    ;-)

    --
    Cheers,
    Simon B,
    ,
    http://www.brunningonline.net/simon/blog/
    Simon Brunning, Jul 17, 2006
    #3
  4. PTY

    Steve Holden Guest

    PTY wrote:
    > Which is better?
    >
    > lst = [1,2,3,4,5]
    >
    > while lst:
    > lst.pop()
    >
    > OR
    >
    > while len(lst) > 0:
    > lst.pop()
    >

    The former, without a doubt. It says exactly the same thing, since lst
    can only be considered false when it is empty. Experienced Python
    programmers would scratch their heads at your second formulation.

    I doubt there's much in it from a time point of view (though I know as I
    write this it will spur someone to use timeit.py to point out I am wrong).

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://holdenweb.blogspot.com
    Recent Ramblings http://del.icio.us/steve.holden
    Steve Holden, Jul 17, 2006
    #4
  5. PTY

    dwelch91 Guest

    PTY wrote:
    > Which is better?
    >
    > lst = [1,2,3,4,5]
    >
    > while lst:
    > lst.pop()
    >
    > OR
    >
    > while len(lst) > 0:
    > lst.pop()
    >


    I think the first one is better, but if all you are doing is removing
    all the items in the list, this is definitely better:

    lst = []

    -Don
    dwelch91, Jul 17, 2006
    #5
  6. PTY

    Tim Chase Guest

    > lst = [1,2,3,4,5]
    > while lst:
    > lst.pop()
    >
    > Or even just:
    >
    > lst = []


    Subtly different though...

    >>> while lst:

    .... lst.pop()
    ....
    5
    4
    3
    2
    1
    >>> lst2

    []
    >>> lst = [1,2,3,4,5]
    >>> lst2 = lst
    >>> lst = []
    >>> lst2

    [1, 2, 3, 4, 5]
    >>> lst = [1,2,3,4,5]
    >>> lst2 = lst
    >>> del lst[:]
    >>> lst2

    []

    The original while loop changes the actual list, reassigning it
    to a new list prevents other items that reference that list from
    accessing the changes. As shown above, I recommend

    del lst[:]

    which should be as fast as python will let one do it. (maybe?
    again with those timeit guys... ;)

    -tkc
    Tim Chase, Jul 17, 2006
    #6
  7. PTY

    tac-tics Guest

    > Or even just:
    >
    > lst = []
    >
    > ;-)


    Indeed.

    I'd say the second one. Empty lists are not false. They are empty. Long
    live dedicated boolean data types.
    tac-tics, Jul 17, 2006
    #7
  8. PTY

    dwelch91 Guest

    tac-tics wrote:
    >
    > I'd say the second one. Empty lists are not false. They are empty. Long
    > live dedicated boolean data types.
    >

    Uh, no, empty lists are False in a boolean context:

    http://docs.python.org/lib/truth.html

    -Don
    dwelch91, Jul 17, 2006
    #8
  9. PTY

    Steve Holden Guest

    tac-tics wrote:
    >>Or even just:
    >>
    >>lst = []
    >>
    >>;-)

    >
    >
    > Indeed.
    >
    > I'd say the second one. Empty lists are not false. They are empty. Long
    > live dedicated boolean data types.
    >

    Take them off to where they belong!

    I'll bet you still write

    if a>3 == True:

    don't you ;-)

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://holdenweb.blogspot.com
    Recent Ramblings http://del.icio.us/steve.holden
    Steve Holden, Jul 17, 2006
    #9
  10. tac-tics enlightened us with:
    > I'd say the second one. Empty lists are not false.


    Yes they are. See the discussion last week about this topic.

    Sybren
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
    Frank Zappa
    Sybren Stuvel, Jul 17, 2006
    #10
  11. PTY

    Peter Otten Guest

    Steve Holden wrote:

    > I'll bet you still write
    >
    > if a>3 == True:
    >
    > don't you ;-)


    I'll second that.

    if (a>3) == True:

    is the correct way :)

    Peter
    Peter Otten, Jul 17, 2006
    #11
  12. PTY

    Dave Hansen Guest

    On Mon, 17 Jul 2006 17:09:32 +0100 in comp.lang.python, "Simon
    Brunning" <> wrote:

    [...]
    >
    >lst = [1,2,3,4,5]
    >while lst:
    > lst.pop()
    >
    >Or even just:
    >
    >lst = []
    >


    del lst[:]

    is probably closer to what the OP wants...

    Regards,
    -=Dave

    --
    Change is inevitable, progress is not.
    Dave Hansen, Jul 17, 2006
    #12
  13. PTY

    Roger Miller Guest

    Peter Otten wrote:
    > Steve Holden wrote:
    >
    > > I'll bet you still write
    > >
    > > if a>3 == True:
    > >
    > > don't you ;-)

    >
    > I'll second that.
    >
    > if (a>3) == True:
    >
    > is the correct way :)
    >
    > Peter


    No, to be consistent you'll have to write

    if ((a>3) == True) == True:

    Oops, I mean,

    if (((a>3) == True) == True) == True:

    Umm, never mind.
    Roger Miller, Jul 17, 2006
    #13
  14. PTY

    Guest

    PTY wrote:
    > Which is better?
    >
    > lst = [1,2,3,4,5]
    >
    > while lst:
    > lst.pop()
    >
    > OR
    >
    > while len(lst) > 0:
    > lst.pop()


    A dozen posts, but nobody has posted the right
    answer yet, so I will :)

    It doesn't matter -- use whichever you prefer (*)
    This is an angels on the head of a pin issue.

    (*) -- If your code is part of an existing body of
    code that uses one or the other style consistently,
    then you should do the same.
    , Jul 17, 2006
    #14
  15. Roger Miller enlightened us with:
    > No, to be consistent you'll have to write
    >
    > if ((a>3) == True) == True:


    Nah, you want

    def is_true(expression):
    return expression == True

    if is_true((a>3) == True):

    Sybren
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
    Frank Zappa
    Sybren Stuvel, Jul 17, 2006
    #15
  16. PTY

    Donn Cave Guest

    In article <>,
    Steve Holden <> wrote:
    > tac-tics wrote:

    ....
    >> I'd say the second one. Empty lists are not false. They are empty.
    >> Long live dedicated boolean data types.


    > Take them off to where they belong!


    Tac-tics is right, an empty list is not False.

    Anyway, just for some variety, I think (2) is preferrable
    to (1), as is the following

    while 1:
    try:
    lst.pop()
    except IndexError:
    break

    Rather than blindly apply familiar patterns to our work,
    I think everyone would agree that coding style in matters
    like this should follow the underlying point of the code.
    In this case, the body of the test refers implicitly to
    the length of the list, since .pop() -> (list[a], list[:a])
    where a is (len(list) - 1) It's therefore quite appropriate
    for the test to be length.

    Donn Cave,
    Donn Cave, Jul 17, 2006
    #16
  17. Donn Cave wrote:

    > Tac-tics is right, an empty list is not False.


    But that's not what he said. He said it was "not false." That's wrong.
    It's false. It's just not False.

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    The meaning of life is that it stops.
    -- Franz Kafka
    Erik Max Francis, Jul 17, 2006
    #17
  18. PTY

    Bob Greschke Guest

    <> wrote in message
    news:...
    > PTY wrote:
    >> Which is better?
    >>
    >> lst = [1,2,3,4,5]
    >>
    >> while lst:
    >> lst.pop()
    >>
    >> OR
    >>
    >> while len(lst) > 0:
    >> lst.pop()

    >
    > A dozen posts, but nobody has posted the right
    > answer yet, so I will :)
    >
    > It doesn't matter -- use whichever you prefer (*)
    > This is an angels on the head of a pin issue.
    >
    > (*) -- If your code is part of an existing body of
    > code that uses one or the other style consistently,
    > then you should do the same.
    >


    I'd go even one step further. Turn it into English (or your favorite
    non-computer language):

    1. While list, pop.

    2. While the length of the list is greater than 0, pop.

    Which one makes more sense? Guess which one I like. CPU cycles be damned.
    :)

    Bob
    Bob Greschke, Jul 17, 2006
    #18
  19. PTY

    PTY Guest

    Bob Greschke wrote:
    > <> wrote in message
    > news:...
    > > PTY wrote:
    > >> Which is better?
    > >>
    > >> lst = [1,2,3,4,5]
    > >>
    > >> while lst:
    > >> lst.pop()
    > >>
    > >> OR
    > >>
    > >> while len(lst) > 0:
    > >> lst.pop()

    > >
    > > A dozen posts, but nobody has posted the right
    > > answer yet, so I will :)
    > >
    > > It doesn't matter -- use whichever you prefer (*)
    > > This is an angels on the head of a pin issue.
    > >
    > > (*) -- If your code is part of an existing body of
    > > code that uses one or the other style consistently,
    > > then you should do the same.
    > >

    >
    > I'd go even one step further. Turn it into English (or your favorite
    > non-computer language):
    >
    > 1. While list, pop.
    >
    > 2. While the length of the list is greater than 0, pop.
    >
    > Which one makes more sense? Guess which one I like. CPU cycles be damned.
    > :)
    >
    > Bob



    It looks like there are two crowds, terse and verbose. I thought terse
    is perl style and verbose is python style. BTW, lst = [] was not what
    I was interested in :) I was asking whether it was better style to
    use len() or not.
    PTY, Jul 17, 2006
    #19
  20. PTY

    tac-tics Guest

    dwelch91 wrote:
    > tac-tics wrote:
    > >
    > > I'd say the second one. Empty lists are not false. They are empty. Long
    > > live dedicated boolean data types.
    > >

    > Uh, no, empty lists are False in a boolean context:
    >
    > http://docs.python.org/lib/truth.html
    >
    > -Don


    Perhaps I should have specified it like this:

    >>> empty_list = []
    >>> empty_list is not False

    True

    I'm well aware that both of these snippets does the same thing. I'm
    just spouting my opinion that lists and integers are not tests, and in
    an ideal world (Java??? X-) if statements support only boolean types.

    DISCLAIMER: I do not promote the use of Java.
    tac-tics, Jul 18, 2006
    #20
    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. Paul Baxter

    style for coding latches

    Paul Baxter, Aug 10, 2003, in forum: VHDL
    Replies:
    7
    Views:
    1,120
    Mike Treseler
    Aug 15, 2003
  2. Willem Oosthuizen

    Coding style to prioritize certain inputs

    Willem Oosthuizen, Sep 2, 2003, in forum: VHDL
    Replies:
    5
    Views:
    488
    Mike Treseler
    Sep 4, 2003
  3. Analog Guy

    Coding style for CPLD vs FPGA

    Analog Guy, Mar 11, 2005, in forum: VHDL
    Replies:
    3
    Views:
    5,422
    Klaus Falser
    Mar 14, 2005
  4. calmar
    Replies:
    11
    Views:
    756
    calmar
    Feb 21, 2006
  5. Ken Varn
    Replies:
    0
    Views:
    430
    Ken Varn
    Apr 26, 2004
Loading...

Share This Page