Calder Coalson wrote:
>> That is exactly what modules and imports let you do.
> It really doesn't seem to be doing that, because I import a bunch of 
> stuff and define it in my main file, then I import another file and it 
> can't use the global variables and the modules imported in the main file.

Right. Each module has its own namespace. That is not the same as having 
its own interpreter. Any modules and variables you want to use in a 
module have to be imported or otherwise made available to that module.

Python "global" variables are not really global, they have module scope 
- they are only available in the module in which they are defined (or 
imported, which is a way of defining something).

>> It sounds like maybe your program might not be very well organized.
> Probably true, :D  Anyway, what I've done is right a small program to 
> lop all the code together.  Here:
> 
> import os
> 
> final = ""
> def load(name):
>     new = open(os.path.join("Components", name))
>     globals()["final"] = globals()["final"]+new.read()+"\n\n\n\n"
> 
> load("StartCode.py")
> # load more loops here
> load("StartLoop.py")
> 
> program = open(os.path.join("Compiled.py"), "w")
> program.write(final)
> 
> import Compiled
> os.remove(os.path.join("Compiled.py"))
> os.remove(os.path.join("Compiled.pyc"))
> 
> That seems to do the trick.  

Well if you're happy maybe I should leave well enough alone but this 
sure looks ugly to me. I strongly recommend that you learn how to use 
the tools Python gives you to solve this problem. Misunderstanding the 
tool and saying "it doesn't work" and kludging up your own way to solve 
the problem is like trying to use a hammer the wrong way around, finding 
it doesn't work, and using a rock instead. You will be better off in the 
long run learning the right way to use a hammer.

>> Maybe you need to break up your program into functions or classes. 
>> Then some of these functions or classes could be put into separate 
>> modules.
> I'll try to do that in the future, but I'm relatively new to Python, and 
> I'm still learning how I can effectively take advantages of classes and 
> stuff.

Start with functions. My guess is that you could find some bits of 
functionality that could be broken out into functions. Some of the 
functions would probably be related and could go into separate modules.

You might want to join the python-tutor list:
http://mail.python.org/mailman/listinfo/tutor


Kent
_______________________________________________
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig

Reply via email to