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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:32:16+00:00 2026-05-30T08:32:16+00:00

I am using the wonderful sympy in python to generate the augmented system of

  • 0

I am using the wonderful sympy in python to generate the augmented system of an ODE system to calculate the forward sensitivities of the states with respect to to states. My goal is to optimize a system of ODEs. If I have a system of x1…x10 and parameters a1…a5, then the augmented system would have 10+10*5 states. The sympy code I have generates the extra states and assigns them to variables x1…x60.

Later I am using integration in numpy to solve the augmented system. So I have to code a function that returns a the rhs() of the ODE-system – something like this code (if some of you out there is developer at numpy, please correct the bug in two_springs.py, line 29 – m2 is missing).

And my my problem: I want inside a function to assign the variables x1…x_end dynamically (the total number of states would change depending how many parameter I use for the sensitivities). I was thrilled then I found the built-in locals() function in python. From this post I thought this should work:

def test_l(w, t):
for ii in range(len(w)):
    varStr="a%d" % (ii)
    locals()[varStr]=w[ii]
return a1*t+a0
w0 = [1.0, 1.0]
t0 = 1.0
f_x=test_l(w0, t0)
print "func res-> %1.4f\n" % (f_x)

Running the script I am getting global name ‘a1’ is not defined. Later I found out that localst() is actually read only. This what confuses me is that if I debug the function with pde on in ipython, then the variables a1 and a0 actually exist inside the function… Running the code with ‘pdb on’ I am still getting an error, the program execution stops at the return(), but a1 and a0 actually exist in the function’s workspace.

ipdb> a1
1.0

ipdb> a0
1.0

How comes that locals() is read-only, but when debugging with pdb the dictionay can be actually changed?

PS: I solved my problem in this way:

 for ii in range(len(w)):
    #varStr="a%d" % (ii)
    #locals()[varStr]=w[ii]
    varStr="a%d=w[%d]" % (ii, ii)
    exec(varStr)
 return a1*t+a0
  • 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-30T08:32:18+00:00Added an answer on May 30, 2026 at 8:32 am

    The reason for the warning in the Python documentation against writing to locals() that jdi mentions is that most of the time, things you change in locals() don’t get propagated back to the actual local variables. That’s because locals can come from more than one place. The reason locals() is a function in the first place, rather than a dictionary, is so that there can be some code that gathers all the local variables from the places where they exist into one place where they can be read conveniently.

    There are occasions when writing to the dictionary returned by locals() works. If you include an exec statement anywhere in your function, even if it’s never executed (i.e. is after a return statement), writing to locals() will work. However, local variables will then be slower than otherwise because Python can’t use the “fast” opcodes to access local variables by index when it doesn’t know at “compile” time the names of all the local variables.

    def foo(val):    # circuitously returns value passed in
        locals["b"] = val
        return b
        exec ""
    

    Also, you will then find that closures (sharing of variables between an inner and outer function) don’t work.

    def foo(a):   # can't define this function
        def bar():
           return a
        return bar
        exec ""
    

    I believe that the Python interpreter also does some extra magic with locals() while a trace function is active.

    This is all specific to CPython; Jython and IronPython probably behave differently.

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

Sidebar

Related Questions

Python has this wonderful way of handling string substitutions using dictionaries: >>> 'The %(site)s
im using linux with gedit which has the wonderful habit of creating a temp
User kokos answered the wonderful Hidden Features of C# question by mentioning the using
Using online interfaces to a version control system is a nice way to have
Using PyObjC , you can use Python to write Cocoa applications for OS X.
Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters
Hi guys I'm using this wonderful class here to do a bit of code
We have a wonderful ASP.NET MVC 2 web application using MS SQL 2008 and
I am using Codeigniter 1.7.2 and Phil Sturgeon's wonderful Dwoo wrapper which allows me
I'm using a wonderful plugin computedstyle.js that gets the computed style of objects. It

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.