Yin Zhu
Yin Zhu

Reputation: 17119

Global variables in Python

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()

Upvotes: 1

Views: 546

Answers (1)

Fang-Pen Lin
Fang-Pen Lin

Reputation: 14406

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

Upvotes: 9

Related Questions