6

I am quite new to python, and I tried to make a simple GUI program. But, I got into a "problem", exactly a warning, that says: 'm' is not defined in the global scope (Python(variable-not-defined-globally)).

I know that you need to declare a var global inside a function if you want to access it outside that function scope. Although I don't use this new created variable outside the function, if I don't declare it global, my program only shows the GUI for a fraction of a second, then it closes it.

import sys
from PyQt5.QtWidgets import QApplication, QWidget
def show():
 global m
 m = QWidget()
 m.setWindowTitle("Testing this app")
 m.show()
MYAPP = QApplication(sys.argv)
show()
MYAPP.exec_()

Could you explain why is this happening? Thanks in advance!

asked Apr 19, 2019 at 17:12
1
  • 1
    this is only a editor warning. You can ignore it. But you should not use global variables. Commented Apr 19, 2019 at 18:19

2 Answers 2

11

global tells python to look for a variable with this name in the global namespace and include it in the local namespace. This means it must exist in the global namespace first.

import sys
from PyQt5.QtWidgets import QApplication, QWidget
m = None # variable must exist in global namespace first
def show():
 global m # this creates a local m that is linked to the global m
 m = QWidget()
 m.setWindowTitle("Testing this app")
 m.show()
MYAPP = QApplication(sys.argv)
show()
MYAPP.exec_()
answered Apr 19, 2019 at 17:18
Sign up to request clarification or add additional context in comments.

5 Comments

that is not what the global statement does. And it absolutely is not required to exist in the global namespace first. This answer is totally wrong, although, it does fix the linter warning. a global statement is basically a directive to the compiler that assignment statements to a variable should modify a global instead of local variable. basically, it means "Use a STORE_GLOBAL opcode instead of a STORE_FAST opcode" if you want to get into the nitty gritty of it
"# this creates a local m that is linked to the global m" no, no it doesn't.
By "must" I simply meant "in order to achieve the requested-for behavior" which is for m not to get freed as soon as the function returned.
look, "global tells python to look for a variable with this name in the global namespace and include it in the local namespace. " is a completely incorrect description of what the global statement does. Fundamentally, that is my gripe. All a global statement does is tell the compiler that assignments should happen to a global rather than a local variable.
And furthermore, it doesn't mean that it has to exist in the global namespace first. And m won't get "freed" as soon as the function returns if you omit m = None
0

You should also declare m in the global scope before it is used by show(). You can do this by setting m=None right before you call show().

westr
5996 silver badges20 bronze badges
answered Apr 19, 2019 at 17:17

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.