writing code over several lines

Discussion in 'Python' started by Dominik Kaspar, Oct 17, 2003.

  1. i want to initialize a constant list at the beginning of a python
    program. but the list is too big for one line. is there any
    possibility to spread the list over several lines, so that the code
    looks neat again?

    something like:
    LIST = [[100, 101, 102], [200, 201, 202], [300, 301, 302],
    [400, 401, 402], [500, 501, 502], [600, 601, 602],
    [700, 701, 702], [800, 801, 802], [900, 901, 902]]

    thanks in advance
    dominik
     
    Dominik Kaspar, Oct 17, 2003
    #1
    1. Advertising

  2. Dominik Kaspar

    Roy Smith Guest

    (Dominik Kaspar) wrote:

    > LIST = [[100, 101, 102], [200, 201, 202], [300, 301, 302],
    > [400, 401, 402], [500, 501, 502], [600, 601, 602],
    > [700, 701, 702], [800, 801, 802], [900, 901, 902]]


    Might I suggest trying the above and seeing what happens?
     
    Roy Smith, Oct 17, 2003
    #2
    1. Advertising

  3. Dominik Kaspar

    anton muhin Guest

    Dominik Kaspar wrote:
    > i want to initialize a constant list at the beginning of a python
    > program. but the list is too big for one line. is there any
    > possibility to spread the list over several lines, so that the code
    > looks neat again?
    >
    > something like:
    > LIST = [[100, 101, 102], [200, 201, 202], [300, 301, 302],
    > [400, 401, 402], [500, 501, 502], [600, 601, 602],
    > [700, 701, 702], [800, 801, 802], [900, 901, 902]]
    >
    > thanks in advance
    > dominik


    :) yes. Just type the code and feed it to python interpreter:

    LIST = [[100, 101, 102], [200, 201, 202], [300, 301, 302],
    [400, 401, 402], [500, 501, 502], [600, 601, 602],
    [700, 701, 702], [800, 801, 802], [900, 901, 902]]
    print LIST

    [[100, 101, 102], [200, 201, 202], [300, 301, 302], ...

    regards,
    anton.
     
    anton muhin, Oct 17, 2003
    #3
  4. Just some coding ideas :
    List = [
    [100,101,102]
    , [200,201,202]
    , [300,301,302]
    , .....
    ]
    Can make things clearer and sometimes worse.

    Boris

    On Fri, 17 Oct 2003 06:56:48 -0700, Dominik Kaspar wrote:

    > i want to initialize a constant list at the beginning of a python
    > program. but the list is too big for one line. is there any
    > possibility to spread the list over several lines, so that the code
    > looks neat again?
    >
    > something like:
    > LIST = [[100, 101, 102], [200, 201, 202], [300, 301, 302],
    > [400, 401, 402], [500, 501, 502], [600, 601, 602],
    > [700, 701, 702], [800, 801, 802], [900, 901, 902]]
    >
    > thanks in advance
    > dominik
     
    Boris Boutillier, Oct 17, 2003
    #4
  5. Dominik Kaspar

    Peter Hansen Guest

    Dominik Kaspar wrote:
    >
    > i want to initialize a constant list at the beginning of a python
    > program. but the list is too big for one line. is there any
    > possibility to spread the list over several lines, so that the code
    > looks neat again?
    >
    > something like:
    > LIST = [[100, 101, 102], [200, 201, 202], [300, 301, 302],
    > [400, 401, 402], [500, 501, 502], [600, 601, 602],
    > [700, 701, 702], [800, 801, 802], [900, 901, 902]]


    Did you know that you can run Python directly and get an interactive
    interpreter, where you could try out code like the above and
    see what it does? This and other helpful things are covered in
    the online tutorial at http://www.python.org/doc/current/tut/tut.html

    -Peter
     
    Peter Hansen, Oct 17, 2003
    #5
  6. Dominik Kaspar

    Gary Herron Guest

    > LIST = [[100, 101, 102], [200, 201, 202], [300, 301, 302],
    > [400, 401, 402], [500, 501, 502], [600, 601, 602],
    > [700, 701, 702], [800, 801, 802], [900, 901, 902]]


    There are several ways to do it.

    Anything within [...], (...) or {...} can span lines and needs follow
    NO indent rules. So your example works just as it is.

    Another way is to end any line with a backslash. Then the following
    line is logically appended to the current line, and again its indent
    is irrelevant. I rarely use this because I think it's ugly and because
    I can put almost anything between parentheses.

    Gary Herron
     
    Gary Herron, Oct 17, 2003
    #6
  7. Dominik Kaspar

    rzed Guest

    "Boris Boutillier" <> wrote in message
    news:p...
    > Just some coding ideas :
    > List = [
    > [100,101,102]
    > , [200,201,202]
    > , [300,301,302]
    > , .....
    > ]
    > Can make things clearer and sometimes worse.
    >


    Boris, you are aware that the last member of a Python list can have a
    trailing comma, aren't you? In my opinion, this:
    List = [
    [100,101,102],
    [200,201,202],
    [300,301,302],
    ]
    is at least as clear, and does not force the unnatural binding of a leading
    comma to what follows.

    --
    rzed
     
    rzed, Oct 18, 2003
    #7
  8. Dnia 17 Oct 2003 06:56:48 -0700, Dominik Kaspar napisa³(a):
    > i want to initialize a constant list at the beginning of a python
    > program. but the list is too big for one line. is there any
    > possibility to spread the list over several lines, so that the code
    > looks neat again?
    >
    > something like:
    > LIST = [[100, 101, 102], [200, 201, 202], [300, 301, 302],
    > [400, 401, 402], [500, 501, 502], [600, 601, 602],
    > [700, 701, 702], [800, 801, 802], [900, 901, 902]]


    The above is correct, but use tuples instead.

    --
    [ Wojtek Walczak - gminick (at) underground.org.pl ]
    [ <http://gminick.linuxsecurity.pl/> ]
    [ "...rozmaite zwroty, matowe od patyny dawnosci." ]
     
    Wojtek Walczak, Oct 21, 2003
    #8
  9. Dominik Kaspar

    Peter Hansen Guest

    Wojtek Walczak wrote:
    >
    > Dnia 17 Oct 2003 06:56:48 -0700, Dominik Kaspar napisa³(a):
    > > i want to initialize a constant list at the beginning of a python
    > > program. but the list is too big for one line. is there any
    > > possibility to spread the list over several lines, so that the code
    > > looks neat again?
    > >
    > > something like:
    > > LIST = [[100, 101, 102], [200, 201, 202], [300, 301, 302],
    > > [400, 401, 402], [500, 501, 502], [600, 601, 602],
    > > [700, 701, 702], [800, 801, 802], [900, 901, 902]]

    >
    > The above is correct, but use tuples instead.


    No need. Why do you suggest that? I think the advice goes against
    typical Python programming style in this case.

    -Peter
     
    Peter Hansen, Oct 21, 2003
    #9
  10. Dominik Kaspar

    Jp Calderone Guest

    > Dnia 17 Oct 2003 06:56:48 -0700, Dominik Kaspar napisa?(a):
    > > i want to initialize a constant list at the beginning of a python
    > > program. but the list is too big for one line. is there any
    > > possibility to spread the list over several lines, so that the code
    > > looks neat again?
    > >
    > > something like:
    > > LIST = [[100, 101, 102], [200, 201, 202], [300, 301, 302],
    > > [400, 401, 402], [500, 501, 502], [600, 601, 602],
    > > [700, 701, 702], [800, 801, 802], [900, 901, 902]]


    LIST = [range(x, x + 3) for x in range(100, 1000, 100)]

    blindly-optimizing-the-example-you-give'ly,

    Jp

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.3 (GNU/Linux)

    iD8DBQE/lU02edcO2BJA+4YRAiYcAKCpTaEUC+FvdDpwp2Ajj1gVUV+0rgCeNjSZ
    JrkT9LggBjILOr6ZhpnUNqk=
    =HoH4
    -----END PGP SIGNATURE-----
     
    Jp Calderone, Oct 21, 2003
    #10
  11. Dnia Tue, 21 Oct 2003 09:17:42 -0400, Peter Hansen napisa³(a):
    >> > i want to initialize a constant list at the beginning of a python
    >> > program. but the list is too big for one line. is there any
    >> > possibility to spread the list over several lines, so that the code
    >> > looks neat again?
    >> >
    >> > something like:
    >> > LIST = [[100, 101, 102], [200, 201, 202], [300, 301, 302],
    >> > [400, 401, 402], [500, 501, 502], [600, 601, 602],
    >> > [700, 701, 702], [800, 801, 802], [900, 901, 902]]

    >>
    >> The above is correct, but use tuples instead.

    >
    > No need. Why do you suggest that? I think the advice goes against
    > typical Python programming style in this case.


    Because the OP said: ,,i want to initialize a constant list''.
    If he's sure he want a constant list, he should use tuples to accent
    that the LIST is constant. Besides, tuples are faster than lists.

    --
    [ Wojtek Walczak - gminick (at) underground.org.pl ]
    [ <http://gminick.linuxsecurity.pl/> ]
    [ "...rozmaite zwroty, matowe od patyny dawnosci." ]
     
    Wojtek Walczak, Oct 21, 2003
    #11
  12. Dominik Kaspar

    Peter Hansen Guest

    Wojtek Walczak wrote:
    >
    > Dnia Tue, 21 Oct 2003 09:17:42 -0400, Peter Hansen napisa³(a):
    > >> > i want to initialize a constant list at the beginning of a python
    > >> > program. but the list is too big for one line. is there any
    > >> > possibility to spread the list over several lines, so that the code
    > >> > looks neat again?
    > >> >
    > >> > something like:
    > >> > LIST = [[100, 101, 102], [200, 201, 202], [300, 301, 302],
    > >> > [400, 401, 402], [500, 501, 502], [600, 601, 602],
    > >> > [700, 701, 702], [800, 801, 802], [900, 901, 902]]
    > >>
    > >> The above is correct, but use tuples instead.

    > >
    > > No need. Why do you suggest that? I think the advice goes against
    > > typical Python programming style in this case.

    >
    > Because the OP said: ,,i want to initialize a constant list''.
    > If he's sure he want a constant list, he should use tuples to accent
    > that the LIST is constant. Besides, tuples are faster than lists.


    I disagree. He should instead, as he did, merely use an ALLCAPS
    name for his variable, to identify it as a "constant" list. This is
    considered sufficient in Python.

    Using a tuple just barely "accents" that fact, anyway, since in all
    the cases where the variable is to be used, it is only the name that
    one can see, not the fact that it is a tuple.

    Tuples are also not significantly faster than lists, as I recall
    from past discussions of this. At least, not to the extent that
    it should ever be a consideration when initializing "constant" lists.

    And, again, I believe your advice is contrary to conventional Python
    programming style, though I'm quite sure there are some people who
    would agree with your approach as well...

    -Peter
     
    Peter Hansen, Oct 21, 2003
    #12
  13. Dominik Kaspar

    Paul Rubin Guest

    Peter Hansen <> writes:
    > Tuples are also not significantly faster than lists, as I recall
    > from past discussions of this. At least, not to the extent that
    > it should ever be a consideration when initializing "constant" lists.


    Tuples probably aren't faster, but they do use less memory.
     
    Paul Rubin, Oct 21, 2003
    #13
  14. Dominik Kaspar

    Peter Hansen Guest

    Paul Rubin wrote:
    >
    > Peter Hansen <> writes:
    > > Tuples are also not significantly faster than lists, as I recall
    > > from past discussions of this. At least, not to the extent that
    > > it should ever be a consideration when initializing "constant" lists.

    >
    > Tuples probably aren't faster, but they do use less memory.


    Again, as I recall the past discussions, not significantly so.

    They still have only four bytes (a pointer) per element, and surely for
    any tuple or list where one could possibly be concerned about memory
    consumption the number of elements far outweighs the overhead associated
    with the structure itself (which is probably on the order of a few bytes
    anyway).

    -Peter
     
    Peter Hansen, Oct 21, 2003
    #14
  15. Dominik Kaspar

    Paul Rubin Guest

    Peter Hansen <> writes:
    > > Tuples probably aren't faster, but they do use less memory.

    >
    > Again, as I recall the past discussions, not significantly so.
    >
    > They still have only four bytes (a pointer) per element, and surely for
    > any tuple or list where one could possibly be concerned about memory
    > consumption the number of elements far outweighs the overhead associated
    > with the structure itself (which is probably on the order of a few bytes
    > anyway).


    Lists allocate memory for extra elements, so list.append doesn't have
    to copy the whole list around every time you call it.
     
    Paul Rubin, Oct 21, 2003
    #15
  16. Dominik Kaspar

    Peter Hansen Guest

    Paul Rubin wrote:
    >
    > Peter Hansen <> writes:
    > > > Tuples probably aren't faster, but they do use less memory.

    > >
    > > Again, as I recall the past discussions, not significantly so.
    > >
    > > They still have only four bytes (a pointer) per element, and surely for
    > > any tuple or list where one could possibly be concerned about memory
    > > consumption the number of elements far outweighs the overhead associated
    > > with the structure itself (which is probably on the order of a few bytes
    > > anyway).

    >
    > Lists allocate memory for extra elements, so list.append doesn't have
    > to copy the whole list around every time you call it.


    Ah, I see. How much would that consume in, say, the example as given?
    Is that a significant amount, enough to be concerned about?

    Is there a published algorithm behind this, or is it just some magic
    of the Timbot's that would be found only in the source?

    -Peter
     
    Peter Hansen, Oct 21, 2003
    #16
  17. "Peter Hansen"
    > Is there a published algorithm behind this, or is it just some magic
    > of the Timbot's that would be found only in the source?
    >


    Both ;-)


    From cvs listobject.c...

    /* Round up:
    * If n < 256, to a multiple of 8.
    * If n < 2048, to a multiple of 64.
    * If n < 16384, to a multiple of 512.
    * If n < 131072, to a multiple of 4096.
    * If n < 1048576, to a multiple of 32768.
    * If n < 8388608, to a multiple of 262144.
    * If n < 67108864, to a multiple of 2097152.
    * If n < 536870912, to a multiple of 16777216.
    * ...
    * If n < 2**(5+3*i), to a multiple of 2**(3*i).
    *
    * This over-allocates proportional to the list size, making room
    * for additional growth. The over-allocation is mild, but is
    * enough to give linear-time amortized behavior over a long
    * sequence of appends() in the presence of a poorly-performing
    * system realloc() (which is a reality, e.g., across all flavors
    * of Windows, with Win9x behavior being particularly bad -- and
    * we've still got address space fragmentation problems on Win9x
    * even with this scheme, although it requires much longer lists to
    * provoke them than it used to).

    Emile van Sebille
     
    Emile van Sebille, Oct 21, 2003
    #17
  18. In article <>,
    Peter Hansen <> wrote:
    [ re lists allocating extra space to achieve constant amortized time per
    append operation ]
    > Is there a published algorithm behind this, or is it just some magic
    > of the Timbot's that would be found only in the source?


    The details are in the source, but the same algorithm can be found e.g.
    in Cormen et al, "Introduction to Algorithms" (2nd ed.), section 17.4

    --
    David Eppstein http://www.ics.uci.edu/~eppstein/
    Univ. of California, Irvine, School of Information & Computer Science
     
    David Eppstein, Oct 21, 2003
    #18
  19. Dominik Kaspar

    Peter Hansen Guest

    Emile van Sebille wrote:
    >
    > "Peter Hansen"
    > > Is there a published algorithm behind this, or is it just some magic
    > > of the Timbot's that would be found only in the source?
    > >

    >
    > Both ;-)
    >
    > From cvs listobject.c...
    >
    > /* Round up:
    > * If n < 256, to a multiple of 8.


    Thanks, Emile; Paul was right...

    So the above is probably the extent of the damage. In the example
    given, that would mean instead of (9*3+9)*4 or 144 bytes from the
    element pointers, (9*8+16)*4 or 352 bytes would be consumed instead.
    My guess is that this amount is about on par with the overhead
    associated with the rest of the objects involved, such as the memory
    consumed by the list object itself or by the integer objects.

    Given how many such "constant lists" one creates in an average program
    and how insignificant these numbers really are compared to almost any
    aspect of Python, such as the overhead of the .pyc in memory, I
    wouldn't think this aspect should concern most Python programmers...

    Now, if the items in question were triplets of numbers of some kind,
    such as coordinates, then I'd agree the nine elements should be tuples
    themselves, but the containing thing should still be a list, merely
    for readability and consistency with conventional style.

    Not that any of this amounts to a hill of beans in the greater
    scheme of things....

    -Peter
     
    Peter Hansen, Oct 21, 2003
    #19
  20. Peter Hansen wrote:
    ...
    > They still have only four bytes (a pointer) per element, and surely for
    > any tuple or list where one could possibly be concerned about memory
    > consumption the number of elements far outweighs the overhead associated
    > with the structure itself (which is probably on the order of a few bytes
    > anyway).


    One counterexample from the dark ages:

    map(twoargsfunc, lotsoffirstargs, (onesecondarg,)*len(lotsoffirstargs))

    the memory consumption of all the constructed tuple's elements is
    fixed -- sizeof(onesecondarg) if Python has sizeof (don't you wish...;-),
    as all slots in the tuple point to that one object.

    So, here, the overhead of the structure itself might be important,
    since the elements in that structure aren't; so a tuple MAY be a good
    thing.

    Today we do [twoargsfunc(x,onesecondarg) for x in lotsoffirstargs]
    and we don't worry much about huge tuples made all of the same
    element any more;-). But there may still be a few such cases around.


    Alex
     
    Alex Martelli, Oct 21, 2003
    #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. RootShell
    Replies:
    4
    Views:
    401
    Toby Inkster
    Jul 14, 2004
  2. William Gill
    Replies:
    2
    Views:
    339
    William Gill
    Sep 2, 2005
  3. Victor \Zverok\ Shepelev

    RDOC: several related modules in several C files

    Victor \Zverok\ Shepelev, Mar 6, 2007, in forum: Ruby
    Replies:
    3
    Views:
    200
    Max Lapshin
    Mar 16, 2007
  4. Jan Ask
    Replies:
    6
    Views:
    124
    Jan Ask
    Aug 6, 2007
  5. Replies:
    8
    Views:
    127
    Dr.Ruud
    Jan 28, 2008
Loading...

Share This Page