Thought----by ' in ' ABC ' return True Python member test action

Source: Internet
Author: User

Recently encountered in the judging dictionary whether there is an empty string ", this very good judgment, directly with: ' In [' A ', ' B ', ' C '), it can be directly judged, but when I use the" in "method of the string to judge, I found:" ' in ' ABC ' will still return true, For this problem, has not been aware of the principle, now go to explore and summarize:

First, check the official documentation: Https://docs.python.org/2/reference/expressions.html#not-in

The document is in 5.9.2: Membership test operations is described as follows:

Probably translate the meaning:

"In" and "not" are detection operations on the members of the collection, and if X is in the set S, then x in S returns True, otherwise false is returned. X not in S is the opposite of X in S. The detection of a member is bound to a sequence, and if the collection is a sequence and contains elements that are equal to the object, the object is a member of the collection. However, for many other object types, even if they are not sequences, it is meaningful to support member testing. In particular, dict (key) and sets support membership testing.

  "list" and "meta-ancestor" types: if and only if there is a subscript ' I ' in ' y ' so that x is y[i] or x = = Y[i] is established, X in Y will return True

  "Unicode" and "String" types: to make ' x in Y ' return True, when and only if, ' X ' is a substring of ' y '. There is an equivalent test for y.find (x)! =-1. Note: ' X ' and ' Y ' do not require the same type, for example: U ' ab ' in ' ABC ' also returns True. An empty string is a substring of any string, so "" in ' ABC ' returns TRUE.

  Changed at version 2.3: The previous version, ' X ' is required to be a string of length 1.

  For user-defined classes, the following three scenarios are considered:

A. For user-defined classes with the __contains__ () method: If y.__contains__ (x) is true, X in Y returns true

B. For the __contains__ () method is undefined, but the __iter__ () method is defined: If the value ' Z ' is x==z when iterating over ' y ', then x in Y returns True, and if an exception is thrown during the iteration, the in exception is thrown.

C. For classes that define the __getitem__ () method: If and only if a nonnegative integer subscript i causes x = = Y[i] to be formed, then x in Y returns True, and all lower integer indexes do not throw a Indexerror exception. (An in operation will also occur if any other exception occurs)

The "not in" operation and the "in" Operation produce the opposite result

After reading the official document, for judging the "String" type, you can pass Y.find (x)! =-one to test whether it is true, but this principle is how to come, or want to further explore, the following view Python2.7 Source:

First of all, guess in the source of the first should be related to the object type, and then find the discovery has typeobject.c file, browse and find, contains the following code:

Sqslot ("__contains__", Sq_contains, Slot_sq_contains, Wrap_objobjproc,            " x.__contains__ (y) <==> y in x "),

Then speculate to find the source code related to the __contains__ method and the Find () method mentioned above:

In the Stringlib header file folder, a find.h file is found, and the following function is found in it:

Py_local_inline (int) stringlib_contains_obj (pyobject* str, pyobject* sub) {      return  stringlib_find (        stringlib_str (str), Stringlib_len (str),        0          ! =-1;}

Then go further to find the definition of Stringlib_find ():

py_local_inline (py_ssize_t) stringlib_find (Conststringlib_char*str, py_ssize_t Str_len,Conststringlib_char*Sub, py_ssize_t Sub_len, py_ssize_t offset)    {py_ssize_t pos; if(Str_len <0)        return-1; if(Sub_len = =0)        returnoffset; POS= Fastsearch (str, Str_len, Sub, Sub_len,-1, Fast_search); if(Pos >=0) Pos+=offset; returnPos;}

From here can be seen when str_len = = 0, return offset, and offset has been initialized to 0, from here can also be approximate: Y.find (x)! =-1, when ' x ' is ' empty string ' when the return of 0; when ' x ' exists in ' Y ', return The corresponding offset amount. The next step is to determine whether to return true or false.

Because of the source of the reason for the correlation, the next can be in the stringobject.c file, combined with Find.h for its return value of the principle of view, here is not listed here.

SOURCE directory:

Objects/stringlib/find.h

Objects/typeobject.c

Objects/stringobject.c

Thought----by ' in ' ABC ' return True Python member test action

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.