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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:27:42+00:00 2026-06-09T03:27:42+00:00

I have a function P() . A call to load_variables() should give P the

  • 0

I have a function P(). A call to load_variables() should give P the variable x.
load_variablesshould be able to accept defaults as keyword arguments.

How can this be done?

I have tried the following:

import inspect
def P():
    x = 1
    load_variables(x = 2)
    return x

def load_variables(**kargs):
    stack = inspect.stack()
    try:
        locals_ = stack[1][0].f_locals
    finally:
        del stack
    for __k, __v in kargs.iteritems():
        locals_[__k] = __v

print P() # => should print 2

The x = 1 line shouldn’t actually be there, as I want load_variables() to just bleed the x into P‘s scope.

Is there another, perhaps better, way to do this? What I want is:

  1. Variables have a default value, e.g. x = 2 in the above call to load_variables().
  2. I can overwrite these in load_variables, for instance, load_varibales() has access to a dictionary of variables, and if x is already here, we overwrite it, and spill this x instead of the one given as default argument.
  • 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-09T03:27:43+00:00Added an answer on June 9, 2026 at 3:27 am

    The Python compiler and bytecode interpreter handles, where possible, references to local variables as slots into a defined-size array. This means that if a local variable is not initialised (assigned to) in a scope then the language will not know that a slot for the variable exists in the scope, and will instead look for the variable in an enclosing scope or the global scope. Looking at a disassembly of the function P:

    def P():
        load_variables(x=2)
        return x
    
    dis.dis(P)
      2           0 LOAD_GLOBAL              0 (load_variables)
                  3 LOAD_CONST               1 ('x')
                  6 LOAD_CONST               2 (2)
                  9 CALL_FUNCTION          256
                 12 POP_TOP             
    
      3          13 LOAD_GLOBAL              1 (x)
                 16 RETURN_VALUE        
    

    You can see that without an assignment to x in local scope P will look for it in global scope.

    The right way to do this is to explicitly state which variables you expect the load_variables function to return, using unpacking:

    x, y, z = load_variables(...)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have a function that can wrap any other function call.
I have a problem with the function call: [self dismissModalViewControllerAnimated:YES]; In MainViewController, I can
I have a variable that I'm using to build a JavaScript function call, and
I have a function called call facebook. call_facebook(284328420994); call_facebook(197214710347172); It runs severaltimes It makes
I have the following function call from a thread: Thread Move = new Thread(){
I have function getCartItems in cart.js and I want to call that function in
I have a C program which has a function call that is defined in
I have a function void foo() and inside foo I call the function void
I have the following code in html, I cannot get the Function call back
I have a function that needs to call a virtual method many times in

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.