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 971401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:57:36+00:00 2026-05-16T02:57:36+00:00

Specifically, how are free variables bound at definition for methods of a class? It

  • 0

Specifically, how are free variables bound at definition for methods of a class? It is probably something like this:

  1. enclosing function (temporary) scope => generate closure
  2. global (permanent) scope => generate no closure (just look it up when the method body executes)
  3. raise UnboundLocalError()

Here are two examples:

globalname = 0
class Test(object):
    def method(self):
        print globalname
        print Test

def outer():
    localname = 1
    class Test(object):
        def method(self):
            print globalname
            print localname
            print Test
    return Test

Test().method.__func__.__closure__
# None
outer()().method.__func__.__closure__
# (<cell at 0xb7d655b4: type object at 0x82412bc>, <cell at 0xb7d655cc: int object at 0x81b20b0>)

I couldn’t find much documentation on specifically how they are treated at definition time. Is the above explanation correct?

  • 1 1 Answer
  • 1 View
  • 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-05-16T02:57:37+00:00Added an answer on May 16, 2026 at 2:57 am

    Python assumes a variable is local if and only if it is assigned to, within the current code block. So

    spam = 0
    def ham:
        print( spam )
    

    will make spam a global variable, but

    spam = 0
    def ham:
        spam = 0
        print( spam )
    

    will make a separate variable, local to ham. A closure grabs all the variables local to the enclosing scope. In your first example, there are no local variables so no closure; in the second, localname is assigned to and thus method is a closure.

    As always in Python, there are ways around this assumption. The global keyword declares a variable as global (!), so e.g.

    spam = 0
    def ham:
        global spam
        spam = 0
        print( spam )
    

    will not be a closure. Py3k introduces the nonlocal keyword, which tells Python to look upwards through the scopes until it finds the variable name and refer to that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone explain why this algorithm is free from deadlock specifically? thanks
Can anyone recommend any software control packages (free), specifically for web development. something that
I am looking for open source or free data collaboration software. Specifically this is
This needs to be specifically for indian stock exchange. If its a free one
For glibc 2.15 I was looking at malloc.c, specifically the free() function, and became
Is it possible to develop a Windows driver (specifically a PDF-like printer driver that
This is specifically about GWT's RequestBuilder, but should apply to general XHR as well.
Not specifically a programming question, but might be something a bunch of programmers run
Update: this question is specifically about protecting (encipher / obfuscate) the content client side
I'm looking for a free/open source ecommerce product. Specifically what I need is multisite

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.