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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:18:09+00:00 2026-05-31T19:18:09+00:00

The following (absurd but illustrative) code works as expected by mutating the list defined

  • 0

The following (absurd but illustrative) code works as expected by mutating the list defined in the enclosing function:

def outside1():
    l = list('abcd')
    def inside(a):
        print "Before - %i: %r" % (id(l), l)
        l.append(a)
        print "After - %i: %r\n" % (id(l), l)
    return inside

f = outside1()
[f(c) for c in 'efgh']

This code also works showing that an immutable defined in an enclosing scope is accessible within the enclosed scope:

def outside2():
    t = tuple('abcd')
    def inside():
        print "%i: %r" % (id(t), t)
    return inside

outside2()()

However this fails with local variable 't' referenced before assignment:

def outside3():
    t = tuple('abcd')
    def inside(a):
        print "Before - %i: %r" % (id(t), t)
        t = t + (a,)
        print "After - %i: %r\n" % (id(t), t)
    return inside

f = outside3()
[f(c) for c in 'efgh']

Can someone explain what is going on here? My first guess was that I can mutate but not assign to enclosing scope, but I would have at least expected the before print statement to work given that the outside2 works.

  • 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-05-31T19:18:11+00:00Added an answer on May 31, 2026 at 7:18 pm

    Python detects the scope of names statically while compiling: A name that is assigned to inside a function becomes local to that function. So the line

    t = t + (a,)
    

    renders t local to inside(), and any look-up of t inside inside() will try to look up a local variable of inside(). When the above line is exceuted, t does not exist yet, hence the error.

    In Python 3.x, you can resolve that problem by explicitly declaring t as nonlocal:

    def outside3():
        t = tuple('abcd')
        def inside(a):
            nonlocal t
            print("Before - %i: %r" % (id(t), t))
            t = t + (a,)
            print("After - %i: %r\n" % (id(t), t))
        return inside
    

    All this is completely unrelated to mutability. Your example using lists doesn’t reassign the name l, while the example using a tuple does reassign t; and this is the important difference, not mutability.

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

Sidebar

Related Questions

Following is my javascript code. The problem is when I call the send() function
following piece of code: class a{ public function __get($key) { if($key == 'obj') {
Following code is used to get images from particular folder but how to get
Following s my Code to display data to listview from database. But it will
Following is my JavaScript (mootools) code: $('orderNowForm').addEvent('submit', function (event) { event.preventDefault(); allFilled = false;
My question is, within C++, is the following code defined? Some of it? And
Following is an onclick method called when a button is clicked. But the code
Following XLST code works fine :- <?xml version=1.0?> <xsl:stylesheet xmlns:xsl=http://www.w3.org/1999/XSL/Transform version=1.0> <xsl:template match=/> <xsl:for-each
Following is some obviously-defective code for which I think the compiler should emit a
I have the following code: ... sub setImage { my $self=shift; my $filename=shift; unless(-r

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.