Multiple Files handling with Codependency - Python
Question: I just finished the tutorial for making a rogue-like-game and I'm on my way to
implement freatures.

The problem is, the whole game is a single file with 1k+ lines.

As you can see:

<http://roguebasin.roguelikedevelopment.org/index.php?title=Complete_Roguelike_Tutorial,_using_python%2Blibtcod,_part_13_code>

I want to divide it in different files/folders to handle the implements
better.Maybe a file for each aspect of the game like map/player/npcs/items...
but at least divide in Classes/Functions/Main.

The problem is, when I put this in the Main.py:

    
    
    from Classes import *
    from Functions import *
    

I get

    
    
    NameError: name 'libtcod' is not defined
    

Which is a Module used in Main. Then I tried to import the Main.py in the
Classes.py and Functions.py

And get the

    
    
    NameError: global name 'MAP_WIDTH' is not defined
    

MAP_WIDTH is a global variable in Main.py

I also tried to import the whole Main.py in Classes.py and Functions.py

But I get:

    
    
    NameError: name 'Fighter' is not defined
    

Fighter is a Class inside Classes.py

Can anyone help me sort this out so I can start implement freatures.

EDIT: One simple example is:

Main.py

    
    
    from Functions import *
    def plus_one(ab):
        return ab +1
    
    a = 1
    b = 2
    c = add_things()
    print plus_one(c)
    

Functions.py

    
    
    from Main import *
    def add_things():
        global a,b
        return a + b
    

It's a simple example, but in the project it get a lot of mutual dependecy
between classes/functions and the main file.


Answer: There are many issues with your code and your planned program architecture.
Please read my comment on your post. You need to shore up your knowledge of
object oriented programming.

First, it is highly recommended to never use `from Classes import *`. You
should use `import Classes`. Then to access functions or constants from the
module you would use Classes.function_name or Classes.constant. See for more
info on how to properly import in Python: <http://effbot.org/zone/import-
confusion.htm>

Second, global variables are not recommended in Python. But if you do need
them, you need to remember that in python a `global variable` means global to
a module, not your entire program. Global variables in Python are a strange
beast. If you need to read a global variable nothing special is required.
However if you want to modify a global variable from within a function, then
you must use the `global` keyword.

Thirdly, what you are doing is called a circle dependancy. Module A, imports
Module B and Module B imports Module A. You can define shared functions,
classes etc. in a third Module C. Then both A and B can import Module C. You
can also defined your constants like `MAP_WIDTH` in module C and access them
from A or B with `C.MAP_WIDTH` provided you have an `import C`.

