Some Python 2.1 ideas

Greg Jorgensen gregj at pobox.com
Sun Dec 24 01:34:03 CET 2000


"Bjorn Pettersen" <pbjorn at uswest.net> wrote in message
news:3A452881.7686E717 at uswest.net...
> string.chomp() (a better name would be nice) needs to handle all of:
>
>  - "foo\n"
>  - "foo \n"
>  - "foo \r\n"
>  - "foo \n\r"
>  - "foo"
>
> etc. correctly, which means it should delete between zero and two bytes
> (characters) from the end of the string. This is very different from
> removing leading or trailing whitespace (whitespace can be significant
> you know <wink>).

How should these be handled?

  "foo\n\n\n"
  "foo\n\r\n\r"

etc.

In other words, does the chomp() method always remove 0-2 characters, or all
trailing /n/r characters? Is it a good idea to add a string method that
makes assumptions about what the file.readline and file.readlines methods
return? Wouldn't it be cleaner to enhance the file object with additional
methods:

  f. readline_chomp()
  f.readlines_chomp()

Or perhaps add an attribute to the file object to indicate if newlines
should be stripped for text files?

  f.chomp = 1  # 1=strip trailing /n and /r characters, 0=leave them
(default)

--
Greg Jorgensen
Deschooling Society
Portland, Oregon, USA
gregj at pobox.com





More information about the Python-list mailing list