Python Forum
Python reading variable in another py file wrongly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python reading variable in another py file wrongly
#1
I have 2 .py files below (Both shortened and include the problem sections):
engine.py
import events

running = False;

def start():
    boot() #Can be safely ignored all it does is call pygame.init()
    running = True;

    while running:
        events.update_events()
        if events.exit_request is not None:
            running = False

    #Logic here

    shutdown()
events.py
import pygame

exit_request = None

def update_events():
    for event in pygame.event.get():
        if(event.type == pygame.QUIT):
            exit_request = event
While I am using pygame, that isn't where the problem lies, I have tested it and the problem is that if I access the exit_request variable in the events file from engine.py, it will always show up as None even if there is an exit event, but if I print exit_request from inside events.py, it will show up appropriately as pygame_exit_blah blah blah if the user clicked the x button, but like I said will show up as "None" if printed from engine.py. Does anyone know why this is happening?
Reply
#2
This is a scope problem and has nothing to do event_request being in another file. You would have the same probem if this were all in one file.

The problem is you have two exit_request variables. One in events and one in events.update_events. If you want these to reference the same thing you need to rewrite update_events like this:
def update_events():
    global exit_request
    for event in pygame.event.get():
        if(event.type == pygame.QUIT):
            exit_request = event
This tells Python to use events.exit_request instead of creating events.update_events.exit_request when you do this:
exit_request = event
By default assignment inside a function creates a new variable. If this were not the case there would be no way to make "local" variables. To prevent this from happening you need to define the variable as "global".
Reply
#3
(Nov-20-2020, 11:17 PM)deanhystad Wrote: This is a scope problem and has nothing to do event_request being in another file. You would have the same probem if this were all in one file.

The problem is you have two exit_request variables. One in events and one in events.update_events. If you want these to reference the same thing you need to rewrite update_events like this:
def update_events():
    global exit_request
    for event in pygame.event.get():
        if(event.type == pygame.QUIT):
            exit_request = event
This tells Python to use events.exit_request instead of creating events.update_events.exit_request when you do this:
exit_request = event
By default assignment inside a function creates a new variable. If this were not the case there would be no way to make "local" variables. To prevent this from happening you need to define the variable as "global".
THANK YOU SO MUCH! IT WORKED!!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with reading json file hhchenfx 5 284 Jul-07-2021, 01:58 PM
Last Post: hhchenfx
  passing php variable to python file jerald 1 131 Jul-07-2021, 11:46 AM
Last Post: Larz60+
  [Solved] Reading every nth line into a column from txt file Laplace12 7 466 Jun-29-2021, 09:17 AM
Last Post: Laplace12
  help with project of reading and searching big log file korenron 6 265 Jun-24-2021, 01:57 PM
Last Post: korenron
  Helps with reading csv file - 3 methods hhchenfx 4 480 May-13-2021, 04:15 AM
Last Post: buran
  Subprocess.Popen() not working when reading file path from csv file herwin 13 1,132 May-07-2021, 03:26 PM
Last Post: herwin
  find the header location in a .bin file without reading the whole file at a time SANJIB 0 472 Mar-05-2021, 04:08 PM
Last Post: SANJIB
  Reading a csv file Led_Zeppelin 2 647 Feb-26-2021, 05:48 AM
Last Post: buran
  reading a csv file Led_Zeppelin 3 839 Feb-19-2021, 02:16 PM
Last Post: Led_Zeppelin
  Code not reading http link from .txt file (Beginner level) plarrip 3 575 Dec-17-2020, 11:33 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020