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
Sad problems with reading csv file. MassiJames 3 559 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Reading a file name fron a folder on my desktop Fiona 4 851 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  Trouble with threading and reading variable from a different script Lembas 14 2,841 Apr-26-2023, 11:21 PM
Last Post: Lembas
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,046 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Reading a file JonWayn 3 1,057 Dec-30-2022, 10:18 AM
Last Post: ibreeden
  Reading Specific Rows In a CSV File finndude 3 940 Dec-13-2022, 03:19 PM
Last Post: finndude
  Excel file reading problem max70990 1 865 Dec-11-2022, 07:00 PM
Last Post: deanhystad
  Replace columns indexes reading a XSLX file Larry1888 2 951 Nov-18-2022, 10:16 PM
Last Post: Pedroski55
  Failing reading a file and cannot exit it... tester_V 8 1,753 Aug-19-2022, 10:27 PM
Last Post: tester_V
Sad What did I do wrongly? Tomi 1 897 Aug-07-2022, 08:05 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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