writing code over several lines

Peter Hansen peter at engcorp.com
Tue Oct 21 11:54:11 EDT 2003


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




More information about the Python-list mailing list