Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8334777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:27:33+00:00 2026-06-09T03:27:33+00:00

The code is from the guide of pyquery from pyquery import PyQuery d =

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:27:34+00:00Added an answer on June 9, 2026 at 3:27 am

    This is done via Python’s func_globals magic, which is

    A reference to the dictionary that holds the function’s global variables — the global namespace of the module in which the function was defined.

    If you dive into PyQuery code:

    def func_globals(f):
        return f.__globals__ if PY3k else f.func_globals
    
    def filter(self, selector):
        if not hasattr(selector, '__call__'):
            return self._filter_only(selector, self)
        else:
            elements = []
            try:
                for i, this in enumerate(self):
    
                    # The magic happens here
                    func_globals(selector)['this'] = this
    
                    if callback(selector, i):
                        elements.append(this)
    
            finally:
                f_globals = func_globals(selector)
                if 'this' in f_globals:
                    del f_globals['this']
            return self.__class__(elements, **dict(parent=self))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having this code from http://bash.cyberciti.biz/guide/While_loop , used to read line by line from
This is a code sample taken from the Google Maps API developer's guide. I'm
I have the following code: from random import randint,choice add=lambda x:lambda y:x+y sub=lambda x:lambda
I got this code from our frontend guy for headings: <h2 class=headline><span>Foobar</span></h2> The span
I'm at a loss debugging this code. I copied the example from a guide,
Is there a problem in this example code from the CUDA C Programming Guide?
I have code straight from Google's dev guide: Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse(market://details?id=com.example.android));
The following code snippet is from The Official GNOME 2 Developer's Guide : GMemChunk
Below is the code from internalRegister method of GCMRegistrar class static void internalRegister(Context context,
I am trying to use a snippet of code from a Apple programming guide,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.