I'm trying to make a text-based RPG. I have some code like:
heroes.py:
class Hero():
def __init__(self):
pass
def Attack(self, target):
# ...
def TakeDamage(self, amount):
# ...
monsters.py:
class Monster():
def __init__(self):
pass
def Attack(self, target):
# ...
def TakeDamage(self, amount):
# ...
The whole file structure looks like this:
|__ backend
__init__.py
monsters.py
heroes.py
MainGame.py
Let's say I want Monster
and Hero
to access each other's Attack
and TakeDamage
functions, for example:
class Monster():
def __init__(self):
pass
def Attack(self, target):
# ...
def TakeDamage(self, amount, target:Hero):
damage = # damage calculation here
target.TakeDamage(damage)
How can I do this? So far I've tried:
- Importing each other (e.g.
from .monsters import Monster
) in their respective files - this gives me an error readingImportError: cannot import name 'Monster' from partially initialized module 'backend.monsters' (most likely due to a circular import)
.
Hero.TakeDamage(damage)
, it would be called on a class, nottarget
object and most likely result in an error.class-methods
tag from the question because that is a separate concept from what you appear to be asking about.