writing code over several lines

D

Dominik Kaspar

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
 
R

Roy Smith

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?
 
A

anton muhin

Dominik said:
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.
 
B

Boris Boutillier

Just some coding ideas :
List = [
[100,101,102]
, [200,201,202]
, [300,301,302]
, .....
]
Can make things clearer and sometimes worse.

Boris
 
P

Peter Hansen

Dominik said:
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
 
G

Gary Herron

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
 
R

rzed

Boris Boutillier said:
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.
 
W

Wojtek Walczak

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.
 
P

Peter Hansen

Wojtek said:
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
 
J

Jp Calderone

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-----
 
W

Wojtek Walczak

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.
 
P

Peter Hansen

Wojtek said:
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
 
P

Paul Rubin

Peter Hansen said:
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.
 
P

Peter Hansen

Paul said:
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
 
P

Paul Rubin

Peter Hansen said:
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.
 
P

Peter Hansen

Paul said:
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
 
E

Emile van Sebille

"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
(e-mail address removed)
 
D

David Eppstein

[ 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
 
P

Peter Hansen

Emile said:
"Peter Hansen"

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
 
A

Alex Martelli

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top