Global variable is undefined at the module level

jacksonhaenchen at gmail.com jacksonhaenchen at gmail.com
Fri Nov 22 18:35:24 EST 2019


This is not accurate. I just tested this in python3 and using the global keyword allowed me to declare a variable inside the function that was then visible from the outer scope.

On Tuesday, September 13, 2016 at 6:50:39 AM UTC-5, dieter wrote:
> 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