I'm a very inexperienced programmer creating a game (using Python 3.3) as a learning exercise. I currently have a main module and a combat module.
The people in the game are represented by instances of class "Person", and are created in the main module. However, the combat module obviously needs access to those objects. Furthermore, I'm probably going to create more modules later that will also need access to those objects.
How do I allow other modules to access the Persons from main.py?
As things stand, main.py has
import combat
at the top; adding
import main
to combat.py doesn't seem to help.
Should I instantiate my objects in a separate module (common.py?) and import them to every module that needs to access them?
Yes, you should definitely factor this out. What you tried is circular imports between your modules, and that can be very problematic. If
combat
importsmain
andmain
importscombat
, then you may get an exception becausemain
will not have finished executing whencombat
starts executing for the import. Assumingmain
is your start up script, it should probably do nothing more than instantiate a class or call a method from another module. Avoid global variables, too. Even if it doesn't seem like they'll be a problem now, that can bite you in the behind later on.That said, you can reference members of a module like so:
or
Personally, I generally avoid referencing a method from another module without qualifying it with the module name, but this is also legal:
I typically use
from ... import ...
for classes, and I typically use the first form for methods. (Yes, this sometimes means I have specific class imports from a module in addition to importing the module itself.) But this is only my personal convention.An alternate syntax of which is strongly discouraged is
This will import every member of common into the current scope that does not start with an underscore (
_
). I the reason it's discouraged is because it makes understanding the source of the name and it makes breaking things too easy. Consider this pair of imports:Which module does
SomeClass
come from? There's no way to tell other than to go look at the two modules. Worse, what if both modules defineSomeClass
orSomeClass
is later added tosome_other_module
?if you have imported main module in
combat
module by usingimport main
, then you should use main.*(stuff that are implemented in main module) to access classes and methods in there.example:
also you can use
from main import *
orimport Person
to avoid main.* in the previous.There are some rules for importing modules as described in http://effbot.org/zone/import-confusion.htm :
import X
imports the module X, and creates a reference to that module in the current namespace. Or in other words, after you’ve run this statement, you can useX.name
to refer to things defined in module X.from X import *
imports the module X, and creates references in the current namespace to all public objects defined by that module (that is, everything that doesn’t have a name starting with “_”). Or in other words, after you’ve run this statement, you can simply use a plain name to refer to things defined in module X. But X itself is not defined, soX.name
doesn’t work. And if name was already defined, it is replaced by the new version. And if name in X is changed to point to some other object, your module won’t notice.from X import a, b, c
imports the module X, and creates references in the current namespace to the given objects. Or in other words, you can now usea
andb
andc
in your program.Finally,
X = __import__(‘X’)
works likeimport X
, with the difference that you1) pass the module name as a string, and
2) explicitly assign it to a variable in your current namespace.