Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a function as a parameter - BeautifulSoup

In the BeautifulSoup documentation, a function has been defined as follow:

def has_class_but_no_id(tag):
    return tag.has_attr('class') and not tag.has_attr('id')

And then passed as a parameter to the function: find_all():

soup.find_all(has_class_but_no_id)

Which surprise me is that it worked. I really don't know how the mechanism is working here, how could this function(has_class_but_no_id) return a value for the find_all() function without having a parameter to work on?

like image 639
Hamza Avatar asked Jan 25 '26 05:01

Hamza


1 Answers

has_class_but_no_id is not executing when you pass it to find_all().

find_all performs the call to has_class_but_no_id, multiple times, passing it tags as the value of 'tag' at that time. This is a pattern which takes advantage of the fact that in Python, functions are what's known as first-order objects - they exist as objects, and you can pass them around in variables.

This allows functions to accept other functions and run them at a later time - just like BeautifulSoup is doing here.

Try an experiment:

def say_something(something_to_say):
    print something_to_say

def call_another_function(func, argument):
    func(argument)

call_another_function(say_something, "hi there")

The above answer is taken from this Reddit post.

Moreover, please see the source code for find_all, and call.

like image 127
Emre Sevinç Avatar answered Jan 27 '26 17:01

Emre Sevinç