Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can function which has arguments be called without arguments

Tags:

python

From the code below as you can see mysearch is called without arguments. How is this function call possible? How is it this technique called? Where does the method get it's argument (tag)? I'm sorry I can't find my answer anywhere...

def myserach(tag):
    return tag.has_attr('ResultsAd') # and tag['li']

with open('index.html', 'rb') as file:
    soup = BeautifulSoup(file, "html.parser")

elements1 = soup.find_all('div', attrs={"class": "ResultsAd"})
elements1 = soup.find_all(myserach)
like image 517
J. Doe Avatar asked Jan 28 '23 20:01

J. Doe


2 Answers

The function is not called in your code snippet.

May be this example helps you understand better.

def foo(str):
    print(str)

def bar(arg):
    arg("now calling foo")

bar(foo)
like image 150
sjaymj62 Avatar answered Jan 30 '23 11:01

sjaymj62


It's not "calling a function", it's passing a function name (a function poiter in C terma) as argument to another function that later will call it with appropriate number of parameters.

like image 33
DDS Avatar answered Jan 30 '23 11:01

DDS