interactive help on string functions - howto

Paul McNett p at ulmcnett.com
Wed Sep 29 16:29:54 EDT 2004


Helmut Jarausch writes:

> Still I wonder why I have to prefix it with 'str.'

Because the only builtin identifiers are:

>>> for name in dir(__builtins__):
...     print name
ArithmeticError
AssertionError
... [snip]
vars
xrange
zip

rstrip isn't a builtin, but a method of the str class. str is 
builtin. Hence, to access rstrip(), you have to call it as a 
method of a string class or instance.

>>> s = "mango"
>>> help(s.rstrip)
>>> help(str.rstrip)

Both calls to help will yield the same documentation. help() is 
a builtin function, which is why you don't have to preface that 
with anything.

> I don't need to import str and I don't need to prefix rstrip
> with 'str.' to USE it - why do I have to do so with HELP
> then?

You *do* have to prefix rstrip, with a str class or instance.

>>> rstrip
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'rstrip' is not defined

>>> str.rstrip
<method 'rstrip' of 'str' objects>

>>> s = "cashew"
>>> s.rstrip
<built-in method rstrip of str object at 0x40177780>

Mmmmm. Time for lunch. :)

-- 
Paul McNett
Independent Software Consultant
http://www.paulmcnett.com



More information about the Python-list mailing list