这可能是个愚蠢的问题,但我还是要问它。我有一个生物文件和一个模拟文件,每个文件都包含一个同名的类。在Simulation类的初始化中,我需要初始化两个生物对象。我已经导入了生物文件,但是当我尝试creature.Creature()时,我得到
Traceback (most recent call last):
File "/Users/lego90511/Documents/workspace/creatureSim/simulation.py", line 2, in <module>
import creature
File "/Users/lego90511/Documents/workspace/creatureSim/creature.py", line 3, in <module>
import simulation
File "/Users/lego90511/Documents/workspace/creatureSim/simulation.py", line 86, in <module>
sim = Simulation(5)
File "/Users/lego90511/Documents/workspace/creatureSim/simulation.py", line 6, in __init__
self.creatures = {creature.Creature(4, 4, 3, 4, 4, 3, 10):"", creature.Creature(4, 4, 3, 4, 4, 3, 10):"", }
AttributeError: 'module' object has no attribute 'Creature'"我做错了什么?
相关代码如下:
模拟:
import creature
from random import randint
class Simulation():
def __init__(self, x):
self.creatures = {creature.Creature():"", creature.Creature():"", }
...生物:
class Creature:
def __init__(self, social, intelligence, sensory, speed, bravery, strength, size):
self.traits = [social, intelligence, sensory, speed, bravery, strength]
...发布于 2013-03-25 02:43:08
你有一个循环依赖。creature正在导入simulation,而后者又试图导入creature,因此它失败了。你需要用一种不同的方式来构造你的文件来消除循环--或者把两个类放在一个文件中,或者在一个函数中移动一个导入。
https://stackoverflow.com/questions/15601927
复制相似问题