string() - Is this some kind of sick joke?

John Roth johnroth at ameritech.net
Wed Apr 9 19:00:49 EDT 2003


"Daniel Gowans" <chlump at yahoo.com> wrote in message
news:36dbf824.0304091142.527af834 at posting.google.com...
> There is some sick joke in the python 2.2.2 documentation I have.
> This is located in the ActivePython documentation:
>
> strip(s[, chars])
>   Return a copy of the string with leading and trailing characters
> removed. If chars is omitted or None, whitespace characters are
> removed. If given and not None, chars must be a string; the characters
> in the string will be stripped from the both ends of the string this
> method is called on.
>
> The same goes (in the docs) for rstrip and lstrip.
>
> But does it actually work that way?  No!
>
> Observe from the 2.2.2 interpreter (and 2.2.1 in HPUX):
>
> >>> y="'hello'"
> >>> import string
> >>> string.strip(y,"'")
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in ?
> TypeError: strip() takes exactly 1 argument (2 given)

This is incorrect. The two operand version is a *function* in the string
module, the one operand version is a *method* on the string
type. My copy of the documentation is quite explicit about
the difference. It's consistent with all of the string functions/
methods.

For the string *methods*, see Sequence Types under
Built In Types.

What you gave in the example was the method on the
string type "string.strip([chars])". This only takes one
operand because the string is the string you're calling the
method on.

> >>>
>
> What gives?  Did I get the bogus version?  Is this wishful thinking
> documentation?  I also tried  y.strip("'")  with the same results.

Another poster said that this does appear to be a bug.

John Roth


>
> If this can't be done this way (oh, how I love chomp()) then is there
> a simple, elegant way to do it?

The correct way is to use the method.

John Roth






More information about the Python-list mailing list