interactive help on string functions - howto

Discussion in 'Python' started by Helmut Jarausch, Sep 29, 2004.

  1. Hi,

    entering
    help('rstrip')
    or
    help('ljust')

    into IDLE's shell window I only get
    no Python documentation found ...

    Am I missing something?

    Thanks for a hint,
    Helmut.

    --
    Helmut Jarausch

    Lehrstuhl fuer Numerische Mathematik
    RWTH - Aachen University
    D 52056 Aachen, Germany
     
    Helmut Jarausch, Sep 29, 2004
    #1
    1. Advertising

  2. Helmut Jarausch

    Jeremy Jones Guest

    Helmut Jarausch wrote:

    > Hi,
    >
    > entering
    > help('rstrip')
    > or
    > help('ljust')
    >
    > into IDLE's shell window I only get
    > no Python documentation found ...
    >
    > Am I missing something?
    >
    > Thanks for a hint,
    > Helmut.
    >


    Make sure you've imported string. Then you can do:

    >>> help(string.rstrip)

    Help on function rstrip in module string:

    rstrip(s, chars=None)
    rstrip(s [,chars]) -> string

    Return a copy of the string s with trailing whitespace removed.
    If chars is given and not None, remove characters in chars instead.


    Jeremy Jones
     
    Jeremy Jones, Sep 29, 2004
    #2
    1. Advertising

  3. Helmut Jarausch wrote:
    > entering
    > help('rstrip')
    > or
    > help('ljust')
    >
    > into IDLE's shell window I only get
    > no Python documentation found ...
    >
    > Am I missing something?


    You need to remove quotes from arguments, i.e.,
    >>> help(rstrip) # help('rstrip') is a no-no


    I have to say this error message is really annoying. You can't
    guess what went wrong from it.


    George
     
    George Yoshida, Sep 29, 2004
    #3
  4. Jeremy Jones <zanesdad <at> bellsouth.net> writes:
    > Make sure you've imported string.


    No need to import string, you can use the builtin str:

    >>> help(str.rstrip)

    Help on method_descriptor:

    rstrip(...)
    S.rstrip([chars]) -> string or unicode

    Return a copy of the string S with trailing whitespace removed.
    If chars is given and not None, remove characters in chars instead.
    If chars is unicode, S will be converted to unicode before stripping

    >>> help(str.ljust)

    Help on method_descriptor:

    ljust(...)
    S.ljust(width[, fillchar]) -> string

    Return S left justified in a string of length width. Padding is
    done using the specified fill character (default is a space).


    Steve
     
    Steven Bethard, Sep 29, 2004
    #4
  5. George Yoshida <ml <at> dynkin.com> writes:
    >
    > I have to say this error message is really annoying. You can't
    > guess what went wrong from it.


    Yeah, maybe it would be clearer if it said something like "No Python
    documentation found for module, keyword or topic 'rstrip'" That would make it
    clearer that you can only use a string if the string is the name of a module,
    keyword or a topic (not a function like the OP's example).

    STeve
     
    Steven Bethard, Sep 29, 2004
    #5
  6. Helmut Jarausch

    Jeff Shannon Guest

    George Yoshida wrote:

    > Helmut Jarausch wrote:
    >
    >> entering
    >> help('rstrip')
    >> or
    >> help('ljust')
    >>
    >> into IDLE's shell window I only get
    >> no Python documentation found ...
    >>
    >> Am I missing something?

    >
    >
    > You need to remove quotes from arguments, i.e.,
    > >>> help(rstrip) # help('rstrip') is a no-no



    But note that help(rstrip) doesn't provide any useful help either:

    >>> help(rstrip)

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


    What's needed is help(str.rstrip), which does indeed work. (As already
    pointed out by other posters.)

    Jeff Shannon
    Technician/Programmer
    Credit International
     
    Jeff Shannon, Sep 29, 2004
    #6
  7. Helmut Jarausch <> writes:

    > Hi,
    >
    > entering
    > help('rstrip')
    > or
    > help('ljust')
    >
    > into IDLE's shell window I only get
    > no Python documentation found ...
    >
    > Am I missing something?


    If you install the Python HTML pages in the correct place, it will
    indeed work this way.

    Or you press F1, and enter rstrip in the CHM search box (on Windows).

    Thomas
     
    Thomas Heller, Sep 29, 2004
    #7
  8. Many thanks for the hint.

    Still I wonder why I have to prefix it with 'str.'
    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?

    Steven Bethard wrote:
    > Jeremy Jones <zanesdad <at> bellsouth.net> writes:
    >
    >>Make sure you've imported string.

    >
    >
    > No need to import string, you can use the builtin str:
    >
    >
    >>>>help(str.rstrip)

    >
    > Help on method_descriptor:
    >
    > rstrip(...)
    > S.rstrip([chars]) -> string or unicode
    >
    > Return a copy of the string S with trailing whitespace removed.
    > If chars is given and not None, remove characters in chars instead.
    > If chars is unicode, S will be converted to unicode before stripping
    >
    >
    >>>>help(str.ljust)

    >
    > Help on method_descriptor:
    >
    > ljust(...)
    > S.ljust(width[, fillchar]) -> string
    >
    > Return S left justified in a string of length width. Padding is
    > done using the specified fill character (default is a space).
    >



    --
    Helmut Jarausch

    Lehrstuhl fuer Numerische Mathematik
    RWTH - Aachen University
    D 52056 Aachen, Germany
     
    Helmut Jarausch, Sep 29, 2004
    #8
  9. Many thanks for the hint.

    Still I wonder why I have to prefix it with 'str.'
    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?

    Steven Bethard wrote:
    > Jeremy Jones <zanesdad <at> bellsouth.net> writes:
    >
    >>Make sure you've imported string.

    >
    >
    > No need to import string, you can use the builtin str:
    >
    >
    >>>>help(str.rstrip)

    >
    > Help on method_descriptor:
    >
    > rstrip(...)
    > S.rstrip([chars]) -> string or unicode
    >
    > Return a copy of the string S with trailing whitespace removed.
    > If chars is given and not None, remove characters in chars instead.
    > If chars is unicode, S will be converted to unicode before stripping
    >
    >
    >>>>help(str.ljust)

    >
    > Help on method_descriptor:
    >
    > ljust(...)
    > S.ljust(width[, fillchar]) -> string
    >
    > Return S left justified in a string of length width. Padding is
    > done using the specified fill character (default is a space).
    >



    --
    Helmut Jarausch

    Lehrstuhl fuer Numerische Mathematik
    RWTH - Aachen University
    D 52056 Aachen, Germany
     
    Helmut Jarausch, Sep 29, 2004
    #9
  10. Helmut Jarausch

    Paul McNett Guest

    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
     
    Paul McNett, Sep 29, 2004
    #10
  11. Helmut Jarausch

    Jeff Shannon Guest

    Helmut Jarausch wrote:

    > Many thanks for the hint.
    >
    > Still I wonder why I have to prefix it with 'str.'
    > 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?



    But you *do* prefix rstrip() with *a* str when you use it.

    rstrip() is a string method. Strings are of type 'str'. So an
    expression like

    "my text".rstrip()

    is exactly equivalent to

    str.rstrip("my text")

    as can easily be demonstrated.

    >>> str.rstrip('foo! ')

    'foo!'
    >>> 'foo! '.rstrip()

    'foo!'
    >>>


    Python doesn't know where rstrip() is unless you *tell* it that it's a
    string method. When you're calling it, you're telling it that it's a
    string method by calling it from an instance of type str. When passing
    it to help(), you need to tell it that it's a string method by
    prepending 'str.' to it.

    Jeff Shannon
    Technician/Programmer
    Credit International
     
    Jeff Shannon, Sep 29, 2004
    #11
  12. Helmut> Still I wonder why I have to prefix it with 'str.' I don't need
    Helmut> to import str and I don't need to prefix rstrip with 'str.' to
    Helmut> USE it - why do I have to do so with HELP then?

    You need to prefix rstrip with a string instance or type (class). "str" is
    to readily available type (class). You could also execute

    help("".rstrip)

    Skip
     
    Skip Montanaro, Sep 29, 2004
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. WJ
    Replies:
    2
    Views:
    526
  2. Xiangliang Meng
    Replies:
    1
    Views:
    1,650
    Victor Bazarov
    Jun 21, 2004
  3. Brian van den Broek

    doctests for interactive functions

    Brian van den Broek, Feb 8, 2007, in forum: Python
    Replies:
    2
    Views:
    278
    Brian van den Broek
    Feb 8, 2007
  4. Chris
    Replies:
    3
    Views:
    2,340
  5. James
    Replies:
    4
    Views:
    211
    James
    Jun 15, 2004
Loading...

Share This Page