Discussion:
Global variable is undefined at the module level
(too old to reply)
Daiyue Weng
2016-09-13 10:42:33 UTC
Permalink
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?

cheers
Peter Otten
2016-09-13 11:39:43 UTC
Permalink
Post by Daiyue Weng
Hi, I defined a global variable in some function like this,
global global_var
PyCharm inspection gave me,
Global variable is undefined at the module level
There is no single way to silence that complaint and actually improve your
code; you have to provide some context: Why do you need a global at all, why
can't you (or don't want to) bind it on the module level etc.
Ned Batchelder
2016-09-13 11:47:29 UTC
Permalink
Post by Daiyue Weng
Hi, I defined a global variable in some function like this,
global global_var
PyCharm inspection gave me,
Global variable is undefined at the module level
How to fix this?
It probably wants you to have a line at the top-level of the module (outside of any function) defining the variable:

global_var = None # or some other default value

--Ned.
dieter
2016-09-13 11:50:10 UTC
Permalink
Post by Daiyue Weng
Hi, I defined a global variable in some function like this,
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).
j***@gmail.com
2019-11-22 23:35:24 UTC
Permalink
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.
Post by dieter
Post by Daiyue Weng
Hi, I defined a global variable in some function like this,
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).
Pieter van Oostrum
2019-11-23 12:41:15 UTC
Permalink
Post by j***@gmail.com
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.
What do you mean by that?
Anything other than what was said here?
Post by j***@gmail.com
Post by dieter
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).
... global globvar
... pass
...
Post by j***@gmail.com
Post by dieter
globvar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'globvar' is not defined
Post by j***@gmail.com
Post by dieter
testfunc()
globvar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'globvar' is not defined
... global globvar
... globvar = 1
...
Post by j***@gmail.com
Post by dieter
globvar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'globvar' is not defined
Post by j***@gmail.com
Post by dieter
testfunc()
globvar
1
--
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
Loading...