0

This maybe a dumb question, but I'm gonna ask it anyways. I have a creature file and a simulation file, each contain a class of the same name. In the Simulation class's init, I need to initialize two Creature object. I have imported the creature file, but when I try creature.Creature() I get

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'" 

What am I doing wrong?

Here is the relevant code:

Simulation:

import creature from random import randint class Simulation(): def __init__(self, x): self.creatures = {creature.Creature():"", creature.Creature():"", } ... 

Creature:

class Creature: def __init__(self, social, intelligence, sensory, speed, bravery, strength, size): self.traits = [social, intelligence, sensory, speed, bravery, strength] ... 
3
  • I get no such error when running your code. Commented Mar 24, 2013 at 18:01
  • Running your code on my PC leads to no such error but you should give enough arguments to Creature. Commented Mar 24, 2013 at 18:10
  • I've added the full error text incase that reveals anything thing more. Could it have something to deal with using python 3.3 Commented Mar 24, 2013 at 18:35

1 Answer 1

2

You have a circular dependency. creature is importing simulation, which in turn is trying to import creature, so it fails. You'll need to structure your files a different way to remove the circularity - either put both classes in one file, or move one import inside a function.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.