Python variables are scoped to the innermost function, class, or module in which they're assigned. Control blocks like if and while blocks don't count, so a variable assigned inside an if is still scoped to a function, class, or module.
(Implicit functions defined by a generator expression or list/set/dict comprehension do count, as do lambda expressions. You can't stuff an assignment statement into any of those, but lambda parameters and for clause targets are implicit assignment.)
Python variables are scoped to the innermost function, class, or module in which they're assigned. Control blocks like if and while blocks don't count, so a variable assigned inside an if is still scoped to a function, class, or module.
(Implicit functions defined by a generator expression or list/set/dict comprehension do count, as do lambda expressions. You can't stuff an assignment statement into any of those, but lambda parameters and for clause targets are implicit assignment.)
Yes, they're in the same "local scope", and actually code like this is common in Python:
if condition:
x = 'something'
else:
x = 'something else'
use(x)
Note that x isn't declared or initialized before the condition, like it would be in C or Java, for example.
In other words, Python does not have block-level scopes. Be careful, though, with examples such as
if False:
x = 3
print(x)
which would clearly raise a NameError exception.
if statements don't define a scope in Python.
Neither do loops, with statements, try / except, etc.
Only modules, functions and classes define scopes.
See Python Scopes and Namespaces in the Python Tutorial.
Yes, in Python, variable scopes inside if-statements are visible outside of the if-statement. Two related questions gave an interestion discussion:
Short Description of the Scoping Rules?
and
Python variable scope error
How Does Scope in Python Work?
Can I use a variable initialized in an `if` statement outside of it?
Python life variables in if statement - Stack Overflow
Python: How can I use a variable outside of an if statement
How can I avoid `NameError` exceptions due to variable scope?
Are there any potential pitfalls with Python's scoping rules?
Why doesn't Python adhere to block-level scoping like C or Java?
Trying to understand how scope works in Python. It seems to work a bit differently than in other languages like C++ and Java. When I run the code below
def foo():
y = 5
x = 2
foo()
print(x+y)it returns a name not defined error. That's expected since the scope of y disappeared before it could be used. However, when I run the following code
x = 2
if True:
y = 3
print(x+y)It works just fine. The program identifies y even though it is in another scope. When trying out the same thing in other languages like C++
int x = 2;
if (true)
{
y = 2;
}
cout<<x + y<<endl;it gives a not declared in this scope error. That's what I expected in Python but clearly that's not the case. Python somehow keeps track of variables inside the scope of an if statement or loop even after it exits and the variables were not declared outside of it. Can anyone explain how scope in Python differs from scope in C++ or Java? How does Python treat scope differently?
Python variables are scoped to the innermost function, class, or module in which they're assigned. Control blocks like if and while blocks don't count, so a variable assigned inside an if is still scoped to a function, class, or module. However Implicit functions defined by a generator expression or list/set/dict comprehension do count, as do lambda expressions. You can't stuff an assignment statement into any of those, but lambda parameters and for clause targets are implicit assignment.
Taking into consideration your example:
if 2 < 3:
a = 3
else:
b = 1
print(a)
Note that a isn't declared or initialized before the condition unlike C or Java, In other words, Python does not have block-level scopes. You can get more information about it here
Interpretation and compilation have nothing to do with it, and being interpreted is not a property of languages but of implementations.
You could compile Python and interpret C and get exactly the same result.
In Python, you don't need to declare variables and assigning to a variable that doesn't exist creates it.
With a different condition – if 3 < 2:, for instance – your Python code produces an error.
That is because you cannot guarantee that an if statement will be always true (that would be useless indeed). So the variable might not be initialized, in that case you cannot print the value outside the if statement. If you want to use it you have two choices. 1) initialize the variable outside the if with a default value 2) add and else statement , in that case you will have to create the variable again
The option 1) is the most common
Inside both if statements, you have set the variable prev = res, but in the first snippet of your code, you tried to use the prev without initializing the value of prev within if statement. In this case, you will get the error with the message:
if(res<=num and ((res-num)<(prev-num))):
NameError: name 'prev' is not defined
Solution: Initialize the value of prev before if statement in order to use "and" in if statement.