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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:08:15+00:00 2026-06-15T18:08:15+00:00

What I essentially need is a mutable integer which can accomplish the equivalent of

  • 0

What I essentially need is a mutable integer which can accomplish the equivalent of the below without having to resort to using single element lists.

Below is a contrived code sample which is representative of the essence of my actual use case [1]

>>> a = [5]  
>>> b = [77]  
>>> def swap(num1, num2):  
...     temp = num1[0]  
...     num1[0] = num2[0]  
...     num2[0] = temp  
>>> swap(a, b)  
>>> a  
[77]  
>>> b  
[5]  

[1] My actual use case is closer to this ->
Returning a value after calling a function with a button in Tkinter
I need to return a value from a callback function associated with a Tkinter widget, and I would like to avoid globals.

  • 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-15T18:08:16+00:00Added an answer on June 15, 2026 at 6:08 pm

    Based on the comments, your goal is to get back the return status of a function that you don’t call directly, and you want to avoid globals. In C you’d have no choice but to use a reference, but in python you have much more flexibility than just locals and (C-style) globals: You can use “closures” to let your callback assign to a local variable directly.

    In Python 3

    If you use python 3, you can do this straightforwardly with the nonlocal keyword. The simplest case is when you define your callback on the spot:

    myfunction():
        x = None
        def callback():
            nonlocal x    # Python 3 only 
            x = 5
    
        gtk.somefunction(callback)  # Executes callback()
        print "My callback returned", x
    

    But your callback is probably defined elsewhere, or called from lots of different places? No problem, just define on the spot a wrapper that captures the return value of the real callback:

    def real_callback():
        return 11
    
    def myfunction():
        x = 0 
    
        def wrapper():
            nonlocal x     # Python 3 only
            x = real_callback()
    
        gtk.somefunction(wrapper)
        print "my callback returned", x
    

    This can be obscured by turning wrapper into decorator, but that’s a different matter.

    Python 2 solution

    In python 2 there’s no nonlocal statement, and implicit closures are read-only: If you try the above without the nonlocal statement, you get an error. You can assign to a variable if you declare it global, and that’s all. So, some trickery is necessary:

    First, the function locals() returns a dictionary with all the variables of the local context. locals()['x'] is the local variable x. But locals() is normally read-only. Fortunately there’s a nice (or terrible) work-around: For its own reasons, exec disables the optimization that renders locals() read-only… and, it turns out, it stays disabled for the lifetime of the calling function! (Tested on Python 2.6.6, YMMV. If it doesn’t work, try exec "a = 0" instead). So, you can do it like this:

    def wrapper(callback, context, varname):
        def _callback():
            context[varname] = callback()
        return _callback
    
    def mycode():
        exec ""
        some_library_function(wrapper(real_callback, locals(), 'z'))
        print "The callback returned", z
    

    Is this preferable to just using a mutable container for your return value? That’s a matter of taste, I guess. But you can use the same wrapper any number of times, so in a sense it’s cleaner than the python 3 solution… if you ignore the magic exec.

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

Sidebar

Related Questions

Essentially I want a border-less, black window which I can set the location and
I essentially need to create a version of the following website using JavaScript and
Essentially need to read the dependencies programmatically without loading the assembly itself, as then
Essentially I need to group unique product data into a single row when: The
So I essentially need to do this: String text = line1\n; text += line2\n;
I need to parse a simple statement (essentially a chain of function calls on
Essentially, I need to parse the response string of the CHAT CREATE command with
I've got a bit of a problem. Essentially, I need to store a large
Essentially, I'm looking for a 1D bar-code scanner that I can program, either through
Essentially I have a method of a class called killProgram, which is intended to

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.