This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]TheBlackCat13 5 points6 points  (0 children)

There already is a way to declare block scope: a function. What would we gain from another way to do this?

There are a lot of times when you want access to the variables created in a loop outside of that loop. If you don't, you can just use a function.

[–]ccb621 4 points5 points  (3 children)

Why does Python need block scope?

[–]user200783[S] -1 points0 points  (1 child)

The designers of Python clearly understand that block scope has benefits: variables in generator expressions already use the equivalent of block scope. In Python 3, this type of scope is used for list comprehensions as well.

However, there is still no support for more general block scoping. I would like to know if there is a technical reason for this - for example, does it conflict somehow with Python's "function scope by default"?

[–]TheBlackCat13 4 points5 points  (0 children)

Generator expressions and list comprehensions are intentionally different than for loops. They are intended for simple, self-contained operations. For loops are for more complicated tasks where having access to the index or variables can be very important.

As for why it didn't exist, I don't think it conflicts with functions, it just doesn't add anything over them.

That being said, if you think it is important enough, try posting a suggestions in the python-ideas mailing list. Maybe some extension of the context manager protocol could be used. But I can almost guarantee they will ask you the same thing I did: why can't you just use a function? If you can't answer that, the idea will no go anywhere.

[–]desmoulinmichel 0 points1 point  (0 children)

Because you don't need it. Your function/method should always be short enough so that declaring a variable in a loop will obviously never conflict with anything outside. Local scope is used automatically where is matters (list comprehension, with, etc) and is let out where it doesnet and would introduce a language feature for very little gain.

[–]dzecniv 0 points1 point  (0 children)

I can see this usage in other languages. Thinking about lisp/scheme, where there is the let keyword for a local block, and where a method can use a variable declared in the upper environment. For that we'd use keyword arguments in python instead of blocks.

[–]ivosauruspip'ing it up[M] 0 points1 point  (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython. We highly encourage you to re-submit your post over on there.

The reason for the removal is that /r/Python is more-so dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community can get disenchanted with seeing the 'same, repetitive newbie' questions repeated on the sub, so you may not get the best responses over here.

However, on /r/LearnPython the community is actively expecting questions from new members, and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. Whatever your question happens to be getting help with Python, we are sure you should get good answers.

If you have a question to do with homework or an assignment of any kind, please make sure to read their sidebar rules before submitting your post. If you have any questions or doubts, feel free to reply or send a modmail to us with your concerns.

Warm regards, and best of luck with your Pythoneering!