len(tags)

anonymous wrote:

"len(tags) visits every element in the list"

No, it doesn't. A list knows how long it is without looking at all the elements, it is order (1) and it is fast. It's a fine idea.

There is a nice treatise on the concept of "something vs. nothing" in Python from back when Python first introuced a Bool type:

http://mail.python.org/pipermail/python-list/2002-April/136887.html

Anyway, I think the problem here is the use of None to indicate an error — that's what exceptions are for. I'd be inclined to write it:

def parse_file(filename): """ Parse a file, returning a list of tags. Raise FileParseError None on error. """

f = open(filename,'r') if not check_format(f): raise ParseError # file is wrong format tags = [] for line in f: tags.append( parse_line(line) ) return tags

now you can write:

try: tags = parse_file(file) for tag in tags: do_something except ParseError: print "whoops"


Written in WikklyText.

Reply

The content of this field is kept private and will not be shown publicly.