1

I was looking at this post:

Python BeautifulSoup: wildcard attribute/id search

in which the answer gives a solution:

dates = soup.findAll("div", {"id" : lambda L: L and L.startswith('date')})

I thought I understood the lambda function in python. However, when I look at this lambda L: L and L.startswith('date'), I understand that it ultimately returns an id which has a value that contains 'date'. But why is it written as L and L.startswith('date') ? This looks the lambda function is returning a string and a boolean statement.

Can someone explain the logic behind this please ?

4
  • 2
    Think of it as a filter, you're finding all instances of "div" in the soup that have an attribute "id" that starts with the string "date". The lambda in this case is a predicate function. The "L and L.startswith('date')" is necessary in case L is None, in which case it would throw an exception. Commented Jul 2, 2014 at 12:05
  • @Marcin I see what you mean now and I was wrong. Removed my comments, sorry I caused confusion Commented Jul 2, 2014 at 14:32
  • @Chinegro As you see, you were being misled previously. Commented Jul 2, 2014 at 15:08
  • @TimCastelijns No need to apologise. I have also removed my comments. Commented Jul 2, 2014 at 15:09

2 Answers 2

6

and does not actually return a boolean value, that is it doesn't always return True or False.

What it does is it checks the first value for truthiness. A few things are falsy, like None, or 0, or False, or []. Other things are truthy.

If the first value is falsy, it is returned. If it's truthy, the second value is returned. If you only consider the truthiness value of the result, then that is the short-circuit implementation of the and logical operator.

The reason why it's used in lambda L: L and L.startswith('date') is to make sure this function doesn't throw an exception in case L is None. If it is, the lambda immediately returns None because it is falsy. Without the check, the startswith() call would throw an exception as None doesn't have that method.

Try out the following on the Python prompt:

l = lambda L: L and L.startswith('date')

l(None)
l('')
l('does not start with date')
l('date this one does')
l(0)
l(1)
Sign up to request clarification or add additional context in comments.

1 Comment

Good answer. Depressing that it needs three people to come along before one of them actually understands a core language feature like boolean operators.
1

As the your linked post stated, the lambda acts as a filter. Its not going to find all divs with the ID of the return value of the lambda; that wouldn't be useful because IDs need to be unique.

Instead, soup.findall is only going to find the divs whose ID is validated by the lambda, that is any div whose ID is not empty and starts with the string 'date'.

2 Comments

-1 Lambda does not act as a filter. It's a callable expression.
@Marcin I understand that. However, the lambda function can be used to filter information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.