Python类只接受两个参数?

时间:2014-10-17 15:15:33

标签: python function class import

所以我有一个名为Player()的Python类,它为一个简单的战斗算法提供了总共20个参数。问题是,当我将Player()和战斗功能导入主文件并正确导入所有内容时,我收到此错误:

Traceback (most recent call last):
File "C:\Users\Thomas\Desktop\Paradisium\main_draft.py", line 53, in <module>
     combat(player1,player2)
TypeError: __init__() missing 18 required positional arguments: 'speed', 'agility', 'endurance',           'hitpoints', 'HPregen', 'EPregen', 'originalHP', 'originalEP', 'originalMP', 'MP', 'MGKregen',  'MGKdefence', 'MGKattack', 'debuff', 'buff', 'setType', 'weapon', and 'name'

正如您所看到的,它正确导入并抓取Player()类,由于某种原因,它只接受这里定义的20个变量中的两个:

player1=Player(90,20,50,90,20,1000,.03,1,1000,20,20,20,1,90,100,2,2,3,None,"Somerled Murdoch")

我不知道我输入的是错误还是类似的东西,而且我很确定类中的变量没有限制。

编辑,这是代码:

Player()类:

class Player (object):
def __init__(self, physicalAttack, defence, speed, agility, endurance, hitpoints, HPregen, EPregen, originalHP, originalEP, originalMP, MP, MGKregen, MGKdefence, MGKattack,
             debuff, buff, setType, weapon, name):

combat()功能:

def combat(player1,player2):

将它们导入主文件的行:

import combatAlgorithmDraft
Player=combatAlgorithmDraft.Player
combat=combatAlgorithmDraft.Player

在主文件中调用它们的行:

    player1=Player(90,20,50,90,20,1000,.03,1,1000,20,20,20,1,90,100,2,2,3,None,"Somerled Murdoch")
    player2=Player(60,70,60,50,20,1100,.01,1,1100,20,0,0,0,60,0,2,2,0,0,"The Black Knight")
    combat(player1,player2)

1 个答案:

答案 0 :(得分:1)

combat=combatAlgorithmDraft.Player

你的意思是这样做吗?

combat=combatAlgorithmDraft.combat

在任何情况下,将事物从其他模块导入当前命名空间的传统方法是

from combatAlgorithmDraft import Player, combat