Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Global variable is undefined at the module level

19,971 views
Skip to first unread message

Daiyue Weng

unread,
Sep 13, 2016, 6:42:56 AM9/13/16
to
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

unread,
Sep 13, 2016, 7:40:20 AM9/13/16
to
Daiyue Weng wrote:

> 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

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

unread,
Sep 13, 2016, 7:47:41 AM9/13/16
to
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

unread,
Sep 13, 2016, 7:50:39 AM9/13/16
to
Daiyue Weng <daiyu...@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).

jackson...@gmail.com

unread,
Nov 22, 2019, 6:35:44 PM11/22/19
to
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.

Pieter van Oostrum

unread,
Nov 23, 2019, 7:41:33 AM11/23/19
to
jackson...@gmail.com writes:

> 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?

> On Tuesday, September 13, 2016 at 6:50:39 AM UTC-5, dieter wrote:

>> 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).

Without assignment the variable will not exist in the module namespace:
>>> def testfunc():
... global globvar
... pass
...
>>> globvar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'globvar' is not defined
>>> testfunc()
>>> globvar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'globvar' is not defined

>>> def testfunc():
... global globvar
... globvar = 1
...
>>> globvar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'globvar' is not defined
>>> testfunc()
>>> globvar
1


--
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
0 new messages