[Python-3000] More uniform treatment of files' newlines attribute? (original) (raw)
skip at pobox.com skip at pobox.com
Sun Sep 23 23:07:02 CEST 2007
- Previous message: [Python-3000] decorators for variable assignments?
- Next message: [Python-3000] New io system and binary data
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
While editing the documentation of the builtin open function, I noticed that the newlines attributes can take on three different value types: None, strings or tuples of strings. It seems to me it would be better if was always a set containing the newline values seen so far. There's no testing necessary if you need to do something with the newlines you've seen, you just loop over them:
for nl in f.newlines:
print("%r" % nl)
With the current mixed types metaphor you have to do something like this:
if f.newlines is not None:
if type(f.newlines) is tuple:
for nl in f.newlines:
print("%r" % nl)
else:
print("%r" % f.newlines)
This, of course, assumes the file has been opened in text mode. If you have a binary mode file you also have to call hasattr(f, "newlines"). Presumably in most cases you'll know the file's mode without needing to check, but maybe binary files should also have a newlines attribute which is always the empty set.
Skip
- Previous message: [Python-3000] decorators for variable assignments?
- Next message: [Python-3000] New io system and binary data
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]