Global variable is undefined at the module level

dieter dieter at handshake.de
Tue Sep 13 07:50:10 EDT 2016


Daiyue Weng <daiyueweng at gmail.com> writes:

> Hi, I defined a global variable in some function like this,
>
> def some_function(self):
>
>      global global_var
>
> PyCharm inspection gave me,
>
> Global variable is undefined at the module level
>
> How to fix this?

You define the global variable at the module level.

"global VVV" (in a function) actually does not define "VVV"
as a global variable. Instead, it tells the interpreter that
"VVV" should not be treated as a local variable but be looked up
in the "global" (usually module) namespace.

In order to define "VVV", you must assign a value to it -- either
directly in the "global" (i.e. module) namespace or in your function
(together with a "global VVV" declaration).




More information about the Python-list mailing list