Why does python let you access loop variables out of scope [duplicate] - python

This question already has answers here:
Short description of the scoping rules?
(9 answers)
Closed 3 years ago.
I wrote something like this where I am able to access a variable that is used through pythons in for loop syntax and then being able to access that variable, I was wondering if there is a specific reason to being able to do this
for i in array:
do_something(i)
b = i

From Short description of the scoping rules?: "The for loop does not have its own namespace"
This means that variables declared in the loop are available for the rest of the code in the same scope as the loop itself

Related

Is defining a constant with a loop inside a class compliant with PEP8? [duplicate]

This question already has answers here:
Should class-specific "constants" still be declared at module level?
(2 answers)
Closed 2 years ago.
I would like to use a mathematical serie to define a constant inside a class. I would like to know whether this is compliant with the PEP8. Thank you in advance. Please see image below
That looks fine but SIZE = [2 ** i for i in range(5)] is probably cleaner.

How to assign a variable inside a function with the variable as an input for the function? [duplicate]

This question already has answers here:
How do I pass a variable by reference?
(39 answers)
Closed 3 years ago.
def convert(target):
#Some code here
target = 20
convert(x)
print(x)
I expected the output to be 20, but there is always an error. "NameError: Name x is not defined" Is there some way that I could fix that?
It sounds like you are trying to pass the value by reference, which goes against the grain of Python, which passes variable by assignment. The easiest "solution" is to refactor your code so it returns the new value, as others have suggested.
In some languages (e.g. PHP), you can specify that certain arguments are to be passed by reference (e.g. by prefixing the variable name with a "&"), but in practice this often makes the code harder to read and debug.
For more information, see:
How do I pass a variable by reference?

Why are objects allowed to be used after a block? [duplicate]

This question already has answers here:
Short description of the scoping rules?
(9 answers)
Closed 5 years ago.
In this code, why do a and b not get destroyed at the ends of their respective blocks?
flag = True
if flag:
a = 1
for i in range(2):
b = 2
print(a, b)
Instead, this code prints 1 2. Why does Python allow this? When can I rely on this behavior?
Read up on the scoping rules for Python. In short, a scope is started with a new module: function, method, class, etc. Mere control flow statements (e.g. if and for) do not start a new scope. The scope of a variable is from its firs definition to the end of that scope.
Since this example has only one scope, each variable is good from its first assignment to the end of the file.
Is that enough to clear up the problem?

get variable name of instance [duplicate]

This question already has answers here:
Simpler way to create dictionary of separate variables?
(27 answers)
How can you print a variable name in python? [duplicate]
(8 answers)
Closed 8 years ago.
Can a Python instance get the name of the variable which is used to access the object?
This example code shows what I need:
foo=MyClass()
foo.get_name() --> 'foo'
bar=foo
bar.get_name() --> 'bar'
I know that this is black magic and not clean code. I just want to know if it is possible.
I know that bar.__name__ returns the name, but I need it inside an own method.
How can get_name() be implemented?
This is not a duplicate of questions which answer is __name__

Do iteration variable exist after the iteration statement in python? [duplicate]

This question already has answers here:
Scoping in Python 'for' loops
(8 answers)
Closed 9 years ago.
I have tried this code :
for i in range(10)
print(line, i)
print(line, i)
and the program executed without error, so why is i declared even after the for statement, it must no longer exist.
Yes, your iteration variable isn’t deleted when the loop is finished. As the documentation puts it: „Names in the target list are not deleted when the loop is finished”.
This has to do with variable scopes. As has been pointed out, the variable i exists within the scope of the function you’re in. A loop does not create an extra scope in python.
The scope is within a function, not a loop. A little different than other programming languages.

Categories