No, you misunderstand. Here is what the docs state in context:
In Python, variables that are only referenced inside a function are
implicitly global. If a variable is assigned a value anywhere within
the function’s body, it’s assumed to be a local unless explicitly
declared as global.
Though a bit surprising at first, a moment’s consideration explains
this. On one hand, requiring global for assigned variables provides a
bar against unintended side-effects. On the other hand, if global was
required for all global references, you’d be using global all the
time. You’d have to declare as global every reference to a built-in
function or to a component of an imported module. This clutter would
defeat the usefulness of the global declaration for identifying
side-effects.
Assignment to a name does not require global
. You can use assignment without global
, but the docs are telling you that if you do use assignment in a local scope without the global
directive, the name will be considered a local variable by default! The reasoning is as stated, if assigned-to variables inside a function could be global by default, then you might accidentally assign to a global variable and inadvertently cause a side-effect. But if you require a global
directive, then you will know that your function produces a side-effect. It isn't stating that global
prevents side-effects, it prevents inadvertent side-effects.