The code is from the guide of pyquery
from pyquery import PyQuery
d = PyQuery('<p class="hello">Hi</p><p>Bye</p>')
d('p').filter(lambda i: PyQuery(this).text() == 'Hi')
My question is this
in the 3rd line is an unbound variable and is never defined in current environment, but the above code still works.
How can it work? Why it doesn't complain NameError: name 'this' is not defined
?
It seems that something happens at https://bitbucket.org/olauzanne/pyquery/src/c148e4445f49/pyquery/pyquery.py#cl-478 , could anybody explain it?
This is done via Python's
func_globals
magic, which isIf you dive into PyQuery code: