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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:37:33+00:00 2026-06-11T01:37:33+00:00

Need help with understanding the following sentence from PEP 227 and the Python Language

  • 0

Need help with understanding the following sentence from PEP 227 and the Python Language Reference

If a variable is referenced in an enclosed scope, it is an error to
delete the name. The compiler will raise a SyntaxError for ‘del
name’.

Lack of examples caused I couldn’t reproduce an error at compile time, so an explanation with examples is highly desirable.

  • 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-11T01:37:35+00:00Added an answer on June 11, 2026 at 1:37 am

    The following raises the execption:

    def foo():
        spam = 'eggs'
        def bar():
            print spam
        del spam
    

    because the spam variable is being used in the enclosed scope of bar:

    >>> def foo():
    ...     spam = 'eggs'
    ...     def bar():
    ...         print spam
    ...     del spam
    ... 
    SyntaxError: can not delete variable 'spam' referenced in nested scope
    

    Python detects that spam is being referenced in bar but does not assign anything to that variable, so it looks it up in the surrounding scope of foo. It is assigned there, making the del spam statement a syntax error.

    This limitation was removed in Python 3.2; you are now responsible for not deleting nested variables yourself; you’ll get a runtime error (NameError) instead:

    >>> def foo():
    ...     spam = 'eggs'
    ...     def bar():
    ...         print(spam)
    ...     del spam
    ...     bar()
    ... 
    >>> foo()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 6, in foo
      File "<stdin>", line 4, in bar
    NameError: free variable 'spam' referenced before assignment in enclosing scope
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help with understanding following example with @ModelAttribute from Spring documentation: (method populatePetTypes()
I'm fixing bugs for some application, and I need help understanding the following lines
I need some help understanding the Java memory model.The following is a gerneric example
I need help in understanding when shall I use the following options char *a
I need help with my jcrop preview. I am following this tutorial from railscasts
I need help understanding this type signature which is from the Thrist package. import
I need help in understanding the following C++ code (in a .h file): bool
I need help understanding a practice exam problem for a CS class in python.
Hi need a little help understanding the following statement and the logic of what
I need help understanding the following. Say I have a display that is 854x480

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.