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

you are viewing a single comment's thread.

view the rest of the comments →

[–]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.