Reputation: 23
I am looking at some Web2py code.
The variable tokens
is some kind of a list of strings. To be more precise, it is defined as tokens = form.vars.name.split()
where form.vars.name
is a string.
My question deals with the following instruction :
query = reduce(lambda a,b:a&b,[User.first_name.contains(k)|User.last_name.contains(k) for k in tokens])
Here are my questions :
I know lambda a,b:a&b
defines a function of a
and b
. What is a&b
?
Is the contains
method of User.first_name
specific to Web2py ? Or does it exist in standard Python ?
What is this |
operator in User.first_name.contains(k)|User.last_name.contains(k)
?
What does the reduce
function do ?
Upvotes: 2
Views: 2324
Reputation: 107648
&
and |
are not bitwise and/or here, but are used to build a special object that represents a database query! They correspond to AND
and OR
in SQL statementsUpvotes: 7
Reputation: 89937
&
is the bitwise and operator. The person writing the code almost certainly meant and
, although for boolean values the result is the same.
.contains()
is a method provided by web2py. a.contains(b)
is more pythonically written as b in a
.
|
is the bitwise OR operator. Again, they probably meant or
.
reduce
applies the function given as the first argument to the iterable in the second argument, from left-to-right, first with the first 2 elements, then with the result of that calculation and the third element, etc.
Upvotes: 0
Reputation: 82028
__contains__
, but it does appear in Py3k docs.Upvotes: 0