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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:05:02+00:00 2026-06-17T09:05:02+00:00

The question is vague because the problem is to abuse Python for a good

  • 0

The question is vague because the problem is to abuse Python for a good cause.
Suppose i have python as program input:

scf() # produces F,C that are somehow available globally
ci() # uses F,C 

question is, can scf modify the module of the caller?

and second case:

F,C = scf() # F,C are not set globally.

So basically either capture value (x)or set it globally.
How can it be done?

  • 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-17T09:05:03+00:00Added an answer on June 17, 2026 at 9:05 am

    It makes me ill to share this, but in CPython you can alter the stack frame:

    import sys
    def scf():
        frame = sys._getframe(1) #caller's frame (probably)
        frame.f_globals['F'] = 'F' #add/overwrite globals
        frame.f_globals['C'] = 'C'
    
    def ci():
        print F, C
    
    scf()
    ci() # prints 'F C'
    

    However, why do you need to invisibly alter the namespace of the caller? This reeks of bad design. scf() should return things and ci() should accept those things as arguments. If you share more about what you are doing perhaps we can suggest better approaches.

    Update

    So you’re trying to maintain some state between externally-executed strings which are Python programs. This is trivial to do in Python: simply keep an explicit locals and globals namespace and pass it between invocations of exec and friends.

    s1 = """
    foo = 'bar'
    """
    
    s2 = """
    def myImpureFunction():
        global foo
        foo = 1
    myImpureFunction()
    """
    
    lvars, gvars = {}, {}
    
    exec s1 in lvars, gvars
    print gvars
    exec s2 in lvars, gvars
    print gvars
    

    Perhaps to keep some semblance of sanity you can guarantee to your users that one special var will be persisted between runs:

    s1 = """
    stash.foo = 'bar'
    """
    
    s2 = """
    def myImpureFunction():
        stash.foo = 'foo'
    myImpureFunction()
    """
    
    from types import ModuleType
    
    lvars = {'stash': ModuleType('stash')}
    
    exec s1 in lvars
    print lvars['stash'].foo
    exec s2 in lvars
    print lvars['stash'].foo
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm asking this question because I finally solved a problem that I have been
OK the question title is vague, but here's the problem. I have a list
I asked a question earlier that was closed because it was too vague: Composite
Apologies for the vague question. Here it is: I have a table object created
Sorry for the vague question.. but I'm not sure quite what the problem is.
Sorry about the extremely vague question title (any suggestions for improvements welcome) I have
this question probably wont be explained very well and that's because I don't really
Sorry about the vague question title, but I have these typedefs here: typedef std::list<AnimationKeyframe>
This question have been asked by several people but my problem seems to be
I think this is going to be a vague question because I don't know

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.