Using a class object in a different file

0 Taegan Warren · April 6, 2015
So I have Enemy.py and I have this code inside of it:


class Enemy():

    health = 0
    name = ""

    def __init__(self, health_peram, name_peram):
        self.health = health_peram
        self.name = name_peram

    def get_name(self):
        return self.name

    def get_health(self):
        return self.health


Then I have SpecificEnemy.py and I have this code inside of it:


enemy_1 = Enemy(100, "Jason")
print(enemy_1.get_name())
print(enemy_1.get_health())


Basically I  just want to know how I can create the Enemy object in a different python file.

Post a Reply

Replies

Oldest  Newest  Rating
0 Bernard Pyc0d333 Parah · April 6, 2015
any file that will work will need to have this file imported as a module.
0 Juan Grasso · April 6, 2015
You have to import it in SpecificEnemy.py. Just with the line:
import Enemy
0 Taegan Warren · April 7, 2015
I tried with the imports and it still was not working. It was actually that when I hit 'run program' it was running the enemy.py and not specificenemy.py. Everything is good now though. Thanks guys!
  • 1

Python

107,352 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator