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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:49:24+00:00 2026-05-17T00:49:24+00:00

Does python have an equivalent to Tcl’s uplevel command? For those who don’t know,

  • 0

Does python have an equivalent to Tcl’s uplevel command? For those who don’t know, the “uplevel” command lets you run code in the context of the caller. Here’s how it might look in python:

def foo():
    answer = 0
    print "answer is", answer # should print 0
    bar()
    print "answer is", answer # should print 42


def bar():
    uplevel("answer = 42")

It’s more than just setting variables, however, so I’m not looking for a solution that merely alters a dictionary. I want to be able to execute any code.

  • 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-17T00:49:25+00:00Added an answer on May 17, 2026 at 12:49 am

    In general, what you ask is not possible (with the results you no doubt expect). E.g., imagine the “any code” is x = 23. Will this add a new variable x to your caller’s set of local variables, assuming you do find a black-magical way to execute this code “in the caller”? No it won’t — the crucial optimization performed by the Python compiler is to define once and for all, when def executes, the exact set of local variables (all the barenames that get assigned, or otherwise bound, in the function’s body), and turn every access and setting to those barenames into very fast indexing into the stackframe. (You could systematically defeat that crucial optimization e.g. by having an exec '' at the start of every possible caller — and see your system’s performance crash through the floor in consequence).

    Except for assigning to the caller’s local barenames, exec thecode in thelocals, theglobals may do roughly what you want, and the inspect module lets you get the locals and globals of the caller in a semi-reasonable way (in as far as deep black magic — which would make me go postal on any coworker suggesting it be perpetrated in production code — can ever be honored with the undeserved praise of calling it “semi-reasonable”, that is;-).

    But you do specify “I want to be able to execute any code.” and the only solution to that unambiguous specification (and thanks for being so precise, as it makes answering easier!) is: then, use a different programming language.

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

Sidebar

Related Questions

Possible Duplicate: Python Ternary Operator Does Python have an equivalent of the ternary operator?:
Does Python have a function equivalent to scheme's apply? I notice that Python's reduce
Does C++ have any equivalent to python's function os.path.join ? Basically, I'm looking for
Just as the title asks, does C++ have the equivalent of Python's setitem and
Does anybody know if Python has an equivalent to Java's SortedSet interface? Heres what
Does Python or any of its modules have an equivalent of MATLAB's conv2 function?
Does python have immutable lists? Suppose I wish to have the functionality of an
Does Python have a built-in, simple way of encoding/decoding strings using a password? Something
Does Python have a pool of all strings and are they (strings) singletons there?
As python does not have concept of constants, would it be possible to raise

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.