My limited brain cannot understand why this happens:
>>> print '' in 'lolsome'
True
In PHP, a equivalent comparison returns false:
var_dump(strpos('', 'lolsome'));
My limited brain cannot understand why this happens:
>>> print '' in 'lolsome'
True
In PHP, a equivalent comparison returns false:
var_dump(strpos('', 'lolsome'));
From above, it seems PHP treats the empty string as a valid string.
But it doesn't seem to consider the empty string as fully legitimate one. Most probably PHP is the only language which doesn't allow the substring to be searched within a string to be an empty string.
Is it a defensive mechanism? Obviously, programmers don't have to protect the needle with
if
. If so, why other languages allow this test to pass!!! Language designers have to answerWhat's a Python string made up of?
Obviously The empty string has one empty string.
One element string has two empty srings.
So it seems Python string is concatenation of one element strings. Each element in a string is sandwiched between two empty strings.
But here Python contradicts the validity of the empty string. Is it a bug?
Ruby and JavaScript pass the test here.
I've compiled several language examples from Rosetta code, it's interesting to note that they all allow the empty string in substring search and return true.
AWK
C
C++
C#
Clojure
Go
Groovy
returns 0, on error returns -1
Java
JavaScript
Lua
Perl
Python
Ruby
From the documentation:
From looking at your
print
call, you're using 2.x.To go deeper, look at the bytecode:
COMPARE_OP
is where we are doing our boolean operation and looking at the source code forin
reveals where the comparison happens:and where cmp_outcome is in the same file, it's easy to find our next clue:
which is in abstract.c:
and to come up for air from the source, we find this next function in the documentation:
and further down in the same documentation:
Where
''
isn'tnull
, the sequence'lolsome'
can be thought to contain it.Suppose you have 2 piles of similar objects, say, best stanzas of your favorite poet, of 5 and 2 respectively. Does bigger set contains smaller set? How to check: 1) for any stanza in the smaller pile you may find it in a bigger one. 2) smaller pile doesn't contain something which is absent in bigger one.
So we may use this pseudocode to check:
If you haven't found such an object, you come to the end of algo, and think smaller is a subset of bigger.
Now imagine smaller pile is of 0 stanzas. Applying the same rules above, we perform 0 checks and also do not find object from smaller which is absent in bigger.
So it's correct and handy to deem empty string as a subset of any other string. Even itself. And this is realized in python.
Basically, from math:
The same logic works here. You can consider
''
an empty set. And therefore, it's a subset of every string set, since they must be the same type.But be careful! A subset and a membership are different things.
Quoting from the PHP's
strpos
documentation,So what you have actually tried is similar to the Python construct seen below
So, you should actually have written like shown below to have the corresponding comparison in PHP
Even then it issues a warning and returns
false
.I dug deeper and found the source code corresponding to the
strpos
function,They consider the empty string being searched as a problematic case. So, they are issuing a warning and returning
false
. Apart from this I couldn't find any document discussing why it is being considered as a problem.As far as Python is concerned, this behaviour is well defined in the Comparisons section,