1

Hi I tried the following example (i don't have im in the global) and found that the program actually runs and outputs 10.

Is Python dynamic scoped?

def useGlobal():
    def inSide():
        print 'inside',
        print b
    b = 5
    print im
    inSide()

if __name__ == '__main__':

    im = 10
    useGlobal()
2
  • Why do you need global variables? I can not remember having used global variables in my 18 Python years. Commented Mar 29, 2011 at 9:31
  • 1
    @RestRisiko, that sounds impossible. Note that @Yin's code above uses 3 global variables: __name__, im, and useGlobal. Commented Mar 29, 2011 at 12:02

1 Answer 1

9

The if statement doesn't create another scope in Python, therefore, the "im" is in the module level, namely, the global scope.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.