Python "why" questions
Martin Gregorie
martin at address-in-sig.invalid
Mon Aug 16 07:25:20 EDT 2010
On Mon, 16 Aug 2010 12:33:51 +1200, Gregory Ewing wrote:
> Ian Kelly wrote:
>> On Fri, Aug 13, 2010 at 11:53 AM, Martin Gregorie
>> <martin at address-in-sig.invalid> wrote:
>
>>> real sample[-500:750];
>
>> Ugh, no. The ability to change the minimum index is evil.
>
> Not always; it can have its uses, particularly when you're using the
> array as a mapping rather than a collection.
>
Say you have intensity data captured from an X-ray goniometer from 160
degrees to 30 degrees at 0.01 degree resolution. Which is most evil of
the following?
1) real intensity[16000:3000]
for i from lwb intensity to upb intensity
plot(i/100, intensity[i]);
2) double angle[13000];
double intensity[13000];
for (int i = 0; i < 13000; i++)
plot(angle[i], intensity[i]);
3) struct
{
double angle;
double intensity
} measurement;
measurement m[13000];
for (int i = 0; i < 13000; i++)
plot(m[i].angle, m[i].intensity);
4) double intensity[13000];
for (int i = 0; i < 13000; i++)
plot((16000 - i)/100, intensity[i])
To my mind (1) is much clearer to read and far less error-prone to write,
while zero-based indexing forces you to use code like (2), (3) or (4),
all of which obscure rather than clarify what the program is doing.
--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
More information about the Python-list
mailing list