import issues in Python

Started by
10 comments, last by K1NNY 11 years, 5 months ago
Your import problem is due to a circular dependency (root need letter and letter need root). This could be solved by putting all the shared variables into a third module that you could call common.py and import this common module in both root.py and letter.py.

The problem with your check_input function is that it does not use the global terminal variable but a local copy. Ensure you put the global keyword before using any global variable in a function/method.

If the previous suggestions does not help, upload your complete source package so that we can review it and debug it more comfortably.

Also, from a quick look at your code, I noticed the following problems:

  • you use too much global variables than what is really needed. That makes the code messy and prone to errors and side effects. Makes function and pass the needed variable to those functions

  • using


from x import *
my_function() # it's not clear that my_function come from x.py

makes the code dirty and unreadable. It's hard to tell that you are actually using variables from root.py into your letter module. Prefer using the following form:

import x
x.my_function() # its clear that we use a function from the x module.

  • you should avoid copy pasting code blocks all over the place by putting them into functions . That will improve your code robustness and organisation. ( If you find a bug in a code block that is copy-pasted all over your project, you will have to apply the correction in everywhere the code blocks is copied. If this code blocks is put in a function you just have to fix the body of the function)
  • your code is poorly organised. The idea behind modules is to group common functionnality together. You might have an input module for all your input handling functions, a render module for all your render functions, a config module for your app configuration data and so on...
  • Is root.py the main file? Usually we use the following pattern to define the application entry point:


def main():
print "This the app entry point"

if __name__ == "__main": # only the main script (the one passed to the python interpreter) have __name__ defined to "__main__"
main() # call the main function


Again, this makes the code easier to read. Someone that does not know your project needs to know where the program starts to be able to follow its flow.
I personnaly try to always put code into functions/methods (except the import and global variables obviously).

You could rewrite the beginning of root.py like this:


import pygame
import sys
import textrect
import time
import letter
from letter import *
from ConfigParser import SafeConfigParser
from pygame.locals import *

def main():
pygame.init()
# DISPLAY
DISPLAY = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Root')
# and so on...


if __name__ == "__main__":
main()

Advertisement
Thanks for the help. I planned on moving all the keyboard input events into check_input() i was just testing it first. And i'm still not sure whats causing the display (or the while loops) to update.

This topic is closed to new replies.

Advertisement