I am currently working on the framework for a simple turn based game. I am trying to call a definition within a class Inside of a separate file from my current one. The Program I am importing the moveset file from is called Pymon_Movesets. I am importing it into the Pymon_Main file. The code for both looks a little like this...
(Pymon_Moveset)
class normaltype():
def scratch():
type = normal
slot = 1
# Base normal type move
damage = 2 * level/2
Pymon_Main
From Pymon_Movesets import *
def Initialize():
Scratch = Pymon_Movesets.normaltype.scratch()
Bite = Pymon_Movesets.normaltype.bite()
My Error
File "C:\Users\samsc\Desktop\Pymon\Pymon_main.py", line 2, in <module>
from Pymon_Movesets import * File "C:\Users\samsc\Desktop\Pymon\Pymon_Movesets.py", line 3, in <module>
import Pymon_main File "C:\Users\samsc\Desktop\Pymon\Pymon_main.py", line 110, in <module>
gamefunction.Initialize() File "C:\Users\samsc\Desktop\Pymon\Pymon_main.py", line 26, in Initialize
Scratch = Pymon_Movesets.normaltype.scratch() AttributeError: module 'Pymon_Movesets' has no attribute 'normaltype' The program
'[4908] python.exe' has exited with code -1073741510 (0xc000013a).
I am Using Visual Studios Python Editor.
Thank you for your time
You are importing all the contents of Pymon_Moveset.py
into the current namespace, however, you still call the class using the filename. Also, you need to create an instance of the class before you call a method. Lastly, you need to include self
in the method signature, so it is bound to the class:
In Pymon_Movesets:
class normaltype():
def scratch(self):
type = normal
slot = 1
# Base normal type move
damage = 2 * level/2
In main file:
import Pymon_Movesets
def Initialize():
Scratch = Pymon_Movesets.normaltype().scratch()
Bite = Pymon_Movesets.normaltype().bite()
However, if you want to access the methods in the class using the name of the class and not an instance, use staticmethod
:
In Pymon_Moveset.py
:
class normaltype():
@staticmethod
def scratch():
type = normal
slot = 1
# Base normal type move
damage = 2 * level/2
Since you're doing from Pymon_Movesets import *
, no need to use Pymon_Movesets
to call its normaltype
function if it is a module-level / file-level function.
from Pymon_Movesets import *
def Initialize():
Scratch = normaltype.scratch()
Bite = normaltype.bite()
User contributions licensed under CC BY-SA 3.0