Python - How to reference a global variable from a function [duplicate]
问题 This question already has answers here : How can I access global variable inside class in Python (5 answers) Closed 6 years ago . I must be missing some very basic concept about Python's variable's scopes, but I couldn't figure out what. I'm writing a simple script in which I would like to access a variable that is declared outside the scope of the function : counter = 0 def howManyTimesAmICalled(): counter += 1 print(counter) howManyTimesAmICalled() Unexpectedly for me, when running I get :