How to find "in" in the documentation

T

tinnews

I've had this trouble before, how do I find the details of how "in"
works in the documentation. E.g. the details of:-

if string in bigstring:

It gets a mention in the "if" section but not a lot.
 
A

Albert Hopkins

I've had this trouble before, how do I find the details of how "in"
works in the documentation. E.g. the details of:-

if string in bigstring:

It gets a mention in the "if" section but not a lot.

The operators in and not in test for collection membership. x in
s evaluates to true if x is a member of the collection s, and
false otherwise. x not in s returns the negation of x in s. The
collection membership test has traditionally been bound to
sequences; an object is a member of a collection if the
collection is a sequence and contains an element equal to that
object. However, it make sense for many other object types to
support membership tests without being a sequence. In
particular, dictionaries (for keys) and sets support membership
testing.

For the list and tuple types, x in y is true if and only if
there exists an index i such that x == y is true.

For the Unicode and string types, x in y is true if and only if
x is a substring of y. An equivalent test is y.find(x) != -1.
Note, x and y need not be the same type; consequently, u'ab' in
'abc' will return True. Empty strings are always considered to
be a substring of any other string, so "" in "abc" will return
True.

Changed in version 2.3: Previously, x was required to be a
string of length 1.

For user-defined classes which define the __contains__() method,
x in y is true if and only if y.__contains__(x) is true.

For user-defined classes which do not define __contains__() and
do define __getitem__(), x in y is true if and only if there is
a non-negative integer index i such that x == y, and all
lower integer indices do not raise IndexError exception. (If any
other exception is raised, it is as if in raised that
exception).
 
T

tinnews

Albert Hopkins said:
I've had this trouble before, how do I find the details of how "in"
works in the documentation. E.g. the details of:-

if string in bigstring:

It gets a mention in the "if" section but not a lot.

The operators in and not in test for collection membership. x in
s evaluates to true if x is a member of the collection s, and
false otherwise. x not in s returns the negation of x in s. The
collection membership test has traditionally been bound to
sequences; an object is a member of a collection if the
collection is a sequence and contains an element equal to that
object. However, it make sense for many other object types to
support membership tests without being a sequence. In
particular, dictionaries (for keys) and sets support membership
testing.

For the list and tuple types, x in y is true if and only if
there exists an index i such that x == y is true.

For the Unicode and string types, x in y is true if and only if
x is a substring of y. An equivalent test is y.find(x) != -1.
Note, x and y need not be the same type; consequently, u'ab' in
'abc' will return True. Empty strings are always considered to
be a substring of any other string, so "" in "abc" will return
True.

Changed in version 2.3: Previously, x was required to be a
string of length 1.

For user-defined classes which define the __contains__() method,
x in y is true if and only if y.__contains__(x) is true.

For user-defined classes which do not define __contains__() and
do define __getitem__(), x in y is true if and only if there is
a non-negative integer index i such that x == y, and all
lower integer indices do not raise IndexError exception. (If any
other exception is raised, it is as if in raised that
exception).

That's what I wanted, thanks, I maybe didn't stare hard enough at the
expressions section.
 
P

Piet van Oostrum

t> I've had this trouble before, how do I find the details of how "in"
t> works in the documentation. E.g. the details of:-
t> if string in bigstring:
t> It gets a mention in the "if" section but not a lot.

It is explained in The Python Language Reference, chapter Expressions,
section Comparisons. At least that's were it is in the 2.6 doc.
 
C

Colin J. Williams

Piet said:
It is explained in The Python Language Reference, chapter Expressions,
section Comparisons. At least that's were it is in the 2.6 doc.

Lots of people have been very helpful
but isn't the OP's real problem
that "in" is not included in the 2.6.1
Help index?

Colin W.
 
G

Gabriel Genellina

That's what I wanted, thanks, I maybe didn't stare hard enough at the
expressions section.

Also, from the interactive interpreter:
Comparisons
***********
[...]

Note that you have to use quotes around "in" because it's a keyword,
help(in) is invalid.
 
J

jkn

Well, this may not solve the OP's problem, but the current
(2.7a0) .chm file has a much better index for operators and
keywords. And "in" is in there. If you're interested in
comparing, there's a copy here:

http://timgolden.me.uk/python/downloads/snapshots/trunk/Python27a0.chm

Thanks for the link (should be a lowercase 'p' - python27a0.chm -
BTW). But having had a look at this file (under kchmviewer rather than
the Windows help viewer) ....

Ye Gods - it's almost unreadable. Not because of the content, but
because of the page style. I'm getting black text on a sort of slate
blue background. Is this the expected appearance?

Jon N
 
T

tinnews

Colin J. Williams said:
Lots of people have been very helpful
but isn't the OP's real problem
that "in" is not included in the 2.6.1
Help index?
Yes, I think that is/was my problem. To find the information I had
to know that I should look in the Expressions chapter and while that
is (possibly) obvious for some things (+, -, AND, OR, etc.) it isn't
quite so obvious for "in".
 
T

Tim Golden

jkn said:
Thanks for the link (should be a lowercase 'p' - python27a0.chm -
BTW). But having had a look at this file (under kchmviewer rather than
the Windows help viewer) ....

Ye Gods - it's almost unreadable. Not because of the content, but
because of the page style. I'm getting black text on a sort of slate
blue background. Is this the expected appearance?

Ummm. No. It uses the same css as the standard Python docs.
Frankly I think the contrast could be better, but it's
certainly readable.
Maybe some issue with the kchmviewer rendering? Might be
easier for non-Windows users to look at the dev docs online:

http://docs.python.org/dev/

since that's built from the same source / index. I find the
..chm far easier to search but the online docs are pretty good.

The (js-based) online search feature does work but, unfortunately,
is naive in its selection, so searching for "in" turns up any
page with the word "in" in it! The index page is rather better:

http://docs.python.org/dev/genindex-I.html


Hope that helps

TJG
 
C

Colin J. Williams

Tim said:
Well, this may not solve the OP's problem, but the current
(2.7a0) .chm file has a much better index for operators and
keywords. And "in" is in there. If you're interested in
comparing, there's a copy here:

http://timgolden.me.uk/python/downloads/snapshots/trunk/Python27a0.chm

TJG
Thanks. "in" is now in the index twice,
but I wasn't able to follow the links.

I trust that the improved docs will go
back to 2.6, unless 2.7 is almost ready.

Colin W
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top