re best way to enumerate something in Python

Michael Geary Mike at DeleteThis.Geary.com
Wed May 26 13:18:07 EDT 2004


David Stockwell wrote:
> I'm going to try using the range function.
>
> as in:
>
> ID_COL, ANIMAL_COL, HOUSING_COL = range(3)
>
> This appears to assign assign those vars as 'constants' with values of
0,1,2

Yes, this is exactly the same as if you'd written:

ID_COL = 0
ANIMAL_COL = 1
HOUSING_COL = 2

Earlier in the thread you asked:

> In the os.stat there is aparrently a list of things you can refer to eg:
> ST_SIZE, ST_ATIME, etc.
>
> How are these defined?  They appear to be related to 0,1,2,3,....   some
> sort of enumeration.

It's easy to answer a question like that by searching the Python
installation directory (at least if the names are defined in Python source
code, not C code). You can either do a search, or since these are defined in
the stat module (not os.stat), you can look in stat.py, where you'll find:

# Indices for stat struct members in tuple returned by os.stat()

ST_MODE  = 0
ST_INO   = 1
ST_DEV   = 2
ST_NLINK = 3
ST_UID   = 4
ST_GID   = 5
ST_SIZE  = 6
ST_ATIME = 7
ST_MTIME = 8
ST_CTIME = 9

-Mike





More information about the Python-list mailing list