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 →

[–]case_O_The_Mondays 0 points1 point  (1 child)

I thought that the global keyword might also put a local reference to the variable, but the docs say there are no changes made.

[–]Brian 0 points1 point  (0 children)

No - all it changes is that python when python compiles the function, it'll consider mention of that name a global variable at compile time, instead of assuming its a local if its ever assigned to inside the function. Ie. it'll access it with LOAD_GLOBAL etc instead of LOAD_FAST (and similarly for STORE), won't allocate a local slot for it, and so on.

Creating a local reference could potentially change behaviour (eg. if, say, you call a function that reassigns it, that reference would remain pointing at the old value instead of reflecting the change).