ABOUT FIND

Fredrik Lundh fredrik at pythonware.com
Mon Oct 24 07:22:54 EDT 2005


Shi Mu wrote:

>I got confused by the following information from the help for "FIND":
> find(s, *args)

what "FIND" ?

>>> help(str.find)
Help on method_descriptor:

find(...)
    S.find(sub [,start [,end]]) -> int

    Return the lowest index in S where substring sub is found,
    such that sub is contained within s[start,end].  Optional
    arguments start and end are interpreted as in slice notation.

    Return -1 on failure.

maybe it's string.find?  (that's a backwards compatibility thing; new code
should use the corresponding method instead).

>>> help(string.find)
Help on function find in module string:

find(s, *args)
    find(s, sub [,start [,end]]) -> in

    Return the lowest index in s where substring sub is found,
    such that sub is contained within s[start,end].  Optional
    arguments start and end are interpreted as in slice notation.

    Return -1 on failure.

> what does *args mean (especially the '*')?

help() uses introspection to find the function signature, so the first find()
you're seeing is probably how the function is declared, while the second
one comes from the help string itself.  I guess string.find looks like this:

    def find(s, *args):
        return s.find(*args)

here, the first "*args" means "take a variable number of arguments, and put
the extra arguments in the args variable", the second means "call this method
with arguments taken from the args variable")

but this is an implementation detail (and a limitation in the help system), and
nothing you need to bother with when *using* the find method/function.

> also, in the sub, why put a comma before start?

it means that you should put a comma *between* sub and start if you provide
a start argument, but not add an extra comma if you're only providing a sub
argument.

this would probably be easier to read:

    S.find(sub,  [start, [end]]) -> int

but if the help text looked like that, some nincompoop would most likely complain
that it was broken since you're not really supposed to write:

    S.find(sub,)

(not that it matters in this case; Python ignores trailing commas in function calls,
but they look a bit silly).

> what does 'in' mean?

the method help text says "int", so it looks like a typo, or a cut/paste error.

</F> 






More information about the Python-list mailing list