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

  • Home
  • SEARCH
  • 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 8826975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:16:24+00:00 2026-06-14T07:16:24+00:00

From my understanding, function and class scopes behave pretty much the same: >>> def

  • 0

From my understanding, function and class scopes behave pretty much the same:

>>> def x():
...     a = 123
...     print (locals())
... 
>>> x()
{'a': 123}


>>> class x():
...     a = 123
...     print (locals())
... 
{'a': 123, '__module__': '__main__'}

When, however, I define a closure, behaviors are different. A function simply returns the local binding, as expected:

>>> def x():
...     a = 123
...     t = lambda: a
...     return t
... 
>>> x()()
123

whereas in a class the binding appears to be lost:

>>> class x():
...     a = 123
...     t = lambda self: a
... 
>>> x().t()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in <lambda>
NameError: global name 'a' is not defined

Can anyone explain the discrepancy?

  • 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-14T07:16:25+00:00Added an answer on June 14, 2026 at 7:16 am

    The class scope is a temporary scope, it only exists while executing the body of the class definition. The resulting dict is used to create the class namespace, the __dict__ of the class.

    As far as functions defined in a class are concerned, the next scope ‘up’ is the scope of the class definition itself.

    The following works fine:

    >>> def foo():
    ...     spam = 'eggs'
    ...     class Bar(object):
    ...         def baz(self): return spam
    ...     return Bar()
    ... 
    >>> foo().baz()
    'eggs'
    

    This is documented in pep 227:

    Names in class scope are not accessible. Names are resolved in
    the innermost enclosing function scope. If a class definition
    occurs in a chain of nested scopes, the resolution process skips
    class definitions.

    and in the class compound statement documentation:

    The class’s suite is then executed in a new execution frame (see section Naming and binding), using a newly created local namespace and the original global namespace. (Usually, the suite contains only function definitions.) When the class’s suite finishes execution, its execution frame is discarded but its local namespace is saved. [4] A class object is then created using the inheritance list for the base classes and the saved local namespace for the attribute dictionary.

    Emphasis mine; the execution frame is the temporary scope.

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

Sidebar

Related Questions

From my understanding, the function super should allow a Class that is nested within
I'm having trouble understanding why I can access a property from my parent class,
From my understanding of the manual for DECIMAL in the mysql docs, it states
From my understanding, the soft keyboard is actually a dialog window that underlies all
From my understanding, each of these methods: get() and put() are atomic. But, when
From my understanding of the docs this general approach should work: begin try1 rescue
From my understanding by reading several articles I assumed Process Address Space(PAS) and Virtual
MariaDB 5.3 introduced dynamic columns. From my understanding the next version of mysql should
From what little understanding of Cassandra I have, it seems that data locality is
I have some not understanding actions from gnu clisp Suppose, I have some code

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.