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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:14:02+00:00 2026-06-11T15:14:02+00:00

In reading the specifications for the with statement ( link ), I have some

  • 0

In reading the specifications for the with statement (link), I have some things I’d like to play around with. This isn’t for any production code or anything, I’m just exploring, so please don’t be too harsh if this is a bad idea.

What I’d like to do is grab the piece called “BLOCK” in the linked docs above, and actually tinker around with it inside of the call to __enter__. (See the linked doc, just after the start of the motivation and summary section.)

The idea is to create my own sort of on-the-fly local namespace. Something like this:

with MyNameSpace(some_object):
    print a #Should print some_object.a
    x = 4 #Should set some_object.x=4

Basically, I want the statements inside of the with block to be subordinate to the local variables and assignment conventions of some_object.

In my specific case, some_object might be a special data array that has my own column-wise operations or something. In which case saying something like x = y + 5 if y > 4 else y - 2 might be some fancy NumPy vectorized operation under the hood, but I don’t need to explicitly call some_object‘s interface to those methods. In the namespace, the expressions should “just work” (however I define them to be inferred in the MyNameSpace class.

My first idea is to somehow interrupt the with process and get a hold of the code that goes in the try block. Then interpret that code when __enter__ gets called, and replace the code in the try block with something else (perhaps pass if that would work, but possibly something that restores some_object back to the original variable scope with its new changed variables preserved).

A simple test case would be something like this:

my_dict = {'a':3, 'b':2}
with MyNameSpace(my_dict):
    print a # Should print 3
    x = 5 # When the block finishes, my_dict['x'] should now be 5

I’m interested if this idea exists somewhere already.

I am aware of best practices things for assigning variables. This is a pet project, so please assume that, just for the sake of this idea, we can ignore best practices. Even if you wouldn’t like assigning variables this way, it could be useful in my current project.

Edit

To clarify the kinds of tricky stuff I might want to do, and to address the answer below claiming that it can’t be done, consider the example file testLocals.py below:

my_dict = {'a':1, 'b':2}
m = locals()
print m["my_dict"]['a']
m["my_dict"]['c'] = 3
print my_dict

class some_other_scope(object):
    def __init__(self, some_scope):
        x = 5
        g = locals()
        some_scope.update(g)
        some_scope["my_dict"]["d"] = 4

sos = some_other_scope(m)
print my_dict
print x

which gives the following when I run it non-interactively:

ely@AMDESK:~/Desktop/Programming/Python$ python testLocals.py
1
{'a': 1, 'c': 3, 'b': 2}
{'a': 1, 'c': 3, 'b': 2, 'd': 4}
5
  • 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-11T15:14:03+00:00Added an answer on June 11, 2026 at 3:14 pm

    Try this.

    import sys 
    class MyNameSpace(object):
        def __init__(self,ns):
            self.ns = ns
        def __enter__(self):
            globals().update(self.ns)
        def __exit__(self, exc_type,exc_value,traceback):
            self.ns.update(sys._getframe(1).f_locals)
    
    my_dict = {'a':3, 'b':2} 
    with MyNameSpace(my_dict) as ns:
        print(a) # Should print 3
        x = 5 # When the block finishes, my_dict['x'] should now be 5 
    
    print(my_dict['x'])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been reading about JPA and EJB3 and would like to confirm that
I have been reading through the specification of openGL 1.5, and saw that any
I was reading the Maximum Capacity Specifications for SQL Server and I came across
Reading the Scala by Example book and there is this example when Martin explains
Reading through this excellent article about safe construction techniques by Brain Goetz, I got
I'm reading Understanding Linux Kernel. Paging for 64-bit Architectures As we have seen in
I was re-reading the JSR-133 specification when I wondered if there were any changes
I am reading the EJB 3 in Action book and I have the following
I have been reading ASN.1 specification, but I am not quite sure what exactly
I have been reading the W3C Geolocation API specification and I was wondering if

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.