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
print(i)
This will, unfortunately, work and print whatever i
is. This also plagues for
loops:
for i in range(10):
# do something with i
print(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
else:
a = 6
print(a)
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.
unrelated: when your posts appear in planetpython.org , the title, which should link to the page in your site, points to planetpython.org, so your page cannot be directly reached. RSS glitch ?
LikeLike
Yeah, they’ve told me about that, but I’ve added my site to my own RSS reader and it works fine there. It’s the built-in WordPress RSS system. I don’t know how to solve it.
LikeLike
ive never been enamored with the idea that loop constructs should have scope local to the loop.
if you want local scope, use objects or functions, or use a language perverse enough to do that. scope is enough trouble as it is, but with functions or objects at least theres an upside. maybe use c++ or some “pure” language that is 10 times more tedious to use than a language for mortals.
LikeLike
Just to be sure you know what I mean, the only variable I feel needs to stay only within the loop’s scope is
i
infor i in iterable
.I don’t care a TON about the loop’s case, since loops aren’t built around the idea of creating some sort of variable scope, like Python’s
with
blocks, but it’s still weird to be able to access that variable after the loop since you get access to the last value of the iterable, despite that not having any significant meaning.LikeLiked by 1 person
Not always the goal of an for loop is do do something with each element, sometimes you want to search/find an element and using them afterwards.
LikeLike
True, but I would always assign that to a new variable anyway.
LikeLike
Sheesh, even JS has proper block scope now if you want it.
LikeLike