I would like to know if it is possible to inset an extra scope within my code, without defining a function. The problem that I had was as follows:
def func(a, b, c):
some_var = [a for a in b]
# do stuff with a
My expectation was that a is still the value that I passed to the function, since I expected the other a to be in the scope of the list comprehension (had to search for that bug for ever!).
How can I rewrite it so that the a in the list comprehension is in its own scope?
I would like to have something more elegant than
def func(a, b, c):
def helper(v):
return [a for a in v]
some_var = helper(b)
# do stuff with a
In languages where the scope is defined by using {}
, you could do stuff like
def func(a, b, c) {
some_var = null
{
some_var = [a for a in b]
}
# do stuff with a
}
Is there something similart in Python?