Enums and Python

Martijn Faassen m.faassen at vet.uu.nl
Wed Jun 21 10:16:06 EDT 2000


William Dandreta <wjdandreta at worldnet.att.net> wrote:
> What I am doing is reading a comma delimited text file like this

> line = AB12345,20000621,3.56,...

> fields = splitfields(line,',')

> recognizing that

> fields[Date][:5]  is the year

> is more obvious and less cryptic than

> fields[1][:5]

In many cases I'd use tuple/list unpacking for this:

date, address, name, whatever = string.split(line, ",")

If you don't know how many fields you're going to get, this can be
more tricky. But imagine you know the first 4:

date, address, name, whatever = string.split(line, ",")[:4]

Of course changing a field is harder here. Generally I use the idiom of
constructing new lists (or dictionaries or objects) in that case, 
though. After all, if you're really going to manipulate your fields
it makes sense to wrap them up in objects in any case.

> Trying to put this into a dictionary seems a contrivance to me that doesn't
> make things clearer and only adds overhead.

Pythoneers don't care a lot about overhead. :) But it's true that a dictionary
in this case may not be a lot of help.

Regards,

Martijn
-- 
History of the 20th Century: WW1, WW2, WW3?
No, WWW -- Could we be going in the right direction?



More information about the Python-list mailing list