Linked Questions

33 votes
7 answers
8k views

Why can functions in Python print variables in enclosing scope but cannot use them in assignment? [duplicate]

If I run the following code: x = 1 class Incr: print(x) x = x + 1 print(x) print(x) It prints: 1 2 1 Okay no problems, that's exactly what I expected. And if I do the following: x = 1 ...
Shashank's user avatar
  • 13.9k
56 votes
3 answers
8k views

How references to variables are resolved in Python [duplicate]

This message is a a bit long with many examples, but I hope it will help me and others to better grasp the full story of variables and attribute lookup in Python 2.7. I am using the terms of PEP 227 ...
user3022222's user avatar
11 votes
7 answers
5k views

Can I define a scope anywhere in Python? [duplicate]

Sometimes I find that I have to use functions with long names such as os.path.abspath and os.path.dirname a lot in just a few lines of code. I don't think it's worth littering the global namespace ...
Hubro's user avatar
  • 60k
15 votes
2 answers
6k views

Python nonlocal statement in a class definition [duplicate]

I'm trying to perform some analysis of scope in Python 3 source code and I'm stuck with how the nonlocal statement statement works inside a class definition. As I understand it, the class definition ...
Andyrooger's user avatar
  • 6,746
0 votes
2 answers
31k views

Why "name 'cursor' is not defined" when pyodbc cursor is in main()? [duplicate]

I'm running a Python program, condensed below, from the command line, and it gives me an error (name 'cursor' is not defined) and points to the cursor.fetchmany() row near the bottom. However, the ...
RobinFiveWords's user avatar
0 votes
1 answer
8k views

"local variable referenced before assignment" [duplicate]

I come from a land of heavy Java and PHP experience so when it comes to Python much of the rules do not make sense to me. I have a recursive Fibonacci function below that spits out the error: ...
Nic's user avatar
  • 37
5 votes
2 answers
4k views

Scope of variables declared in main function? [duplicate]

Simple question about variable scope: Why is it that variables declared in the main function are accessible from external functions? I.e. Why does the following print "yes"? def run(): print ...
Stuart C's user avatar
  • 175
5 votes
3 answers
2k views

Class vs instance initialization in Python [duplicate]

I am a bit puzzled why when initializing an instance of a class in Python, I cannot use class attributes. Here is the example: class TestClass: ... shared_list = ['a', 'b', 'c'] ... def __init__(self)...
LazyCat's user avatar
  • 506
-1 votes
2 answers
8k views

NameError: name 'cap' is not defined [duplicate]

I have defined cap in def fingerCursor(device): global cap global cap_height global cap_width cap = cv2.VideoCapture(device) #cap.set(cv2.cap_PROP_FRAME_HEIGHT,720) #...
Spandan Patnaik's user avatar
-1 votes
3 answers
8k views

Class NameError: name 'var' is not defined [duplicate]

class Gui(): var = None def refreshStats(args): print(str(var)) clas = Gui() clas.refreshStats() Trace File "sample.py", line 5, in refreshStats print(str(var)) NameError: name '...
Danil Kondratiev's user avatar
5 votes
1 answer
6k views

How does scope work with try and except blocks in python? [duplicate]

So I am a bit confused about the scoping of variables with try and except blocks. How come my code allows me to use the variables outside of the try block and even the while loop for that matter even ...
Ammar Khazal's user avatar
4 votes
2 answers
3k views

What are the differences (if exist) in variable scope between Python and C++? [duplicate]

I'm a bit confused with the variable scope of Python. Perhaps because of being used to the convention of C++, I always made some mistakes in variable scope of Python. For example: in C++: int main(...
user123's user avatar
  • 241
2 votes
1 answer
3k views

Trying to use variables outside of a for loop gives a SyntaxError: no binding for nonlocal 'max_' found [duplicate]

def min_diff(arry_): max_ =0 temp_ =0 for i in arry_: nonlocal max_ nonlocal temp_ if i > max_: nonlocal max_ nonlocal temp_ temp_ = max_ ...
Shailab Singh's user avatar
7 votes
1 answer
398 views

Python: Variables are still accessible if defined in try or if? [duplicate]

I'm a Python beginner and I am from C/C++ background. I'm using Python 2.7. I read this article: A Beginner’s Guide to Python’s Namespaces, Scope Resolution, and the LEGB Rule, and I think I have ...
yaobin's user avatar
  • 2,586
3 votes
2 answers
854 views

Declare variable as global inside block __name__ == "__main__" [duplicate]

Inside the block if __name__ == "__main__": do I need to declare a variable as global in order to set its value? Or... is this block not a function and hence I don't need to do this? I ...
peter.petrov's user avatar
  • 39.6k

15 30 50 per page
1
2 3 4 5
38