Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Keyword global

A global declaration makes the listed names refer to the module namespace throughout the current code block.

Version: Python 3.14; repository image, production runtime not attested

Syntax

global counter

A global declaration makes the listed names refer to the module namespace throughout the current code block.

Reading a global generally needs no declaration, but assigning the same name in a function normally makes it local. Declare the name before using or assigning it in the block.

An outer function variable is not the global namespace.

Examples

count = 3
def outer():
    count = 9
    def bump():
        global count
        count += 1
    bump()
    return count
print(outer(), count)

Result

9 4

An outer function variable is not the global namespace.

Sources