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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:49:36+00:00 2026-05-13T13:49:36+00:00

I’d like to define a helper function that has the ability to modify a

  • 0

I’d like to define a helper function that has the ability to modify a module-level variable (with known name) from surrounding context without explicitly passing it, e.g.

# mod1.py
mod_var = 1
modify_var()
# mod_var modified
print mod_var

The problem is – I can’t reference variable by mod1.mod_var, because I want to use helper function across many modules (helper itself will be defined in other module); it should dynamically ‘pick’ mod_var from surrounding calling context/scope.

Is this possible? How to obtain this?

My use case is to enhance defining URL -> view mapping in Django. Those definitions are spread across many sub-modules that define urlpatterns module-level variable. Helper function should pick this variable from the module that calls it and modify it. Avoiding explicitly passing it as argument would be great.

Edit:
For additional solution – check this answer.


Edit2:

Wrong solution below! (left for references in comments)

Recently I’ve found another solution (the least magical in my opinion ;))
modify_var() function could be implemented like this:

def modify_var():
    calling_module = __import__("__main__")
    calling_module.mod_var = 42

Still, potential profits are arguable.

unittest module uses this technique in its main method.

  • 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-13T13:49:36+00:00Added an answer on May 13, 2026 at 1:49 pm

    It’s a truly bad, horrible, and awful idea, which will lead to future maintenance nightmares. However, Python does offer “enough rope to shoot yourself in the foot”, if you truly insist: introspection and metaprogramming tools which are mostly intended for debugging purposes, but can be abused to perform the ill-conceived task you so desperately crave.

    For example, in evil.py:

    import inspect
    
    def modify_var():
      callersframe = inspect.stack()[1][0]
      callersglobals = callersframe.f_globals
      if 'mod_var' not in callersglobals:
        raise ValueError, 'calling module has no "mod_var"!'
      callersglobals['mod_var'] += 1
    

    now say you have two modules, a.py:

    import evil
    
    mod_var = 23
    evil.modify_var()
    print 'a mod_var now:', mod_var
    

    and b.py:

    import evil
    
    mod_var = 100
    evil.modify_var()
    print 'b mod_var now:', mod_var
    

    you could do:

    $ python -c'import a; import b'
    a mod_var now: 24
    b mod_var now: 101
    

    However, maintaining this kind of black-magic tricks in the future is going to be a headache, so I’d strongly recommend not doing things this way.

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

Sidebar

Ask A Question

Stats

  • Questions 367k
  • Answers 367k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Never mind, I got it working with the AIR beta… May 14, 2026 at 4:46 pm
  • Editorial Team
    Editorial Team added an answer I believe the answer to all of that is no,… May 14, 2026 at 4:46 pm
  • Editorial Team
    Editorial Team added an answer This vastly depends on your learning speed. What I do… May 14, 2026 at 4:46 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.