A Problem With Python’s Code Blocks

So, this post was going to be completely different. I was going to do a post on how you can restrict the creation and scoping of certain objects using Python’s with code blocks. Unfortunately, with doesn’t restrict scope. Take the following code sample:

with someManager() as i:
    # do something with i

This will, unfortunately, work and print whatever i is. This also plagues for loops:

for i in range(10):
    # do something with i

This will print 9.

I know that there are some people out there that complain about the insides of code blocks not being restricted, like this:

if True:
    a = 5
    a = 6

This, I am okay with, even though it goes against typical scoping rules. I’m okay with this because Python doesn’t have a way to declare a variable (except when declaring that it’s using a global or nonlocal variable), which means we would have to initialize it with a dummy value before writing the if block. I don’t like that, and I also don’t like having to declare a variable prior to a code block. So, I’m okay with the lack of normal “block scoping”.

But, you see, there’s a difference between normal block scoping and allowing the values from the opening lines of a with statement or for loop out. The i values in the earlier with and for examples are implied to only be useful within the blocks they were created for. This is especially true with with blocks. A with block is specifically designed around state control of an object, and the typical object’s state after leaving a with block is unusable. Exiting generally puts the objects into some sort of “closed” state. That’s the whole point of the with block.

The same basic idea also applies to for loops. The iterated element only exists for each iteration, but for some reason, the last one gets to just be set free. I don’t like it. There’s no way they’ll change it at this point, but I wish there was a way.

Anyway, that’s my mini rant now. I’ll probably do a similar article as to what I had planned this week, but just show a different method for accomplishing it.

Reference: A Problem With Python’s Code Blocks from our WCG partner Jacob Zimmerman at the Programming Ideas With Jake blog.

Do you want to know how to develop your skillset to become a Web Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. Building web apps with Node.js

2. HTML5 Programming Cookbook

3. CSS Programming Cookbook

4. AngularJS Programming Cookbook

5. jQuery Programming Cookbook

6. Bootstrap Programming Cookbook


and many more ....


I have read and agree to the terms & conditions


Jacob Zimmerman

Jacob is a certified Java programmer (level 1) and Python enthusiast. He loves to solve large problems with programming and considers himself pretty good at design.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button