属性没有分配给班级? Python 2.7

时间:2018-08-28 00:56:19

标签: python python-2.7 class

在玩文字游戏时,我遇到了一个错误,其中playercharacter没有分配我给它的任何变量。运行示例如下:

What is your name?
Ty

playercharacter.getname()

Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
playercharacter.getname()

AttributeError: 'player' object has no attribute 'getname'

无论我尝试调用哪个属性,它都会返回相同的错误消息。

我在这里做什么错了?

这是下面的完整代码:

class player(object):
    def __init__(self, name, level, ST, PE, EN, CH, IN, AG, LK, location, HP, XP, NXTLVL):
        self.name = name
        self.level = level
        self.ST = ST
        self.PE = PE
        self.EN = EN
        self.CH = CH
        self.IN = IN
        self.AG = AG
        self.LK = LK
        self.smallguns = 2 * AG + LK
        self.bigguns = 2 * ST + LK
        self.unarmed = 2 * EN + LK
        self.meleeweapons = 2 * ST + LK
        self.throwing = 2 * AG + LK
        self.firstaid = 2 * IN + LK
        self.sneak = 2 * AG + LK
        self.lockpick = 2 * PE + LK
        self.steal = 2 * AG + LK
        self.science = 2 * IN + LK
        self.repair = 2 * IN + LK
        self.speech = 2 * CH + LK
        self.location = location
        self.HP = HP
        self.XP = XP
        self.NXTLVL = NXTLVL

        def getname(self):
            return self.name
        def getlevel(self):
            return self.level
        def getST(self):
            return self.ST
        def getPE(self):
            return self.PE
        def getEN(self):
            return self.EN
        def getCH(self):
            return self.CH
        def getIN(self):
            return self.IN
        def getAG(self):
            return self.AG
        def getLK(self):
            return self.LK
        def getsmallguns(self):
            return self.smallguns
        def getbigguns(self):
            return self.bigguns
        def getunarmed(self):
            return self.unarmed
        def getmeleeweapons(self):
            return self.meleeweapons
        def getthrowing(self):
            return self.throwing
        def getfirstaid(self):
            return self.firstaid
        def getsneak(self):
            return self.sneak
        def getlockpick(self):
            return self.lockpick
        def getsteal(self):
            return self.steal
        def getscience(self):
            return self.science
        def getrepair(self):
            return self.repair
        def getspeech(self):
            return self.speech
        def getlocation(self):
            return self.location
        def getHP(self):
            return self.HP
        def getXP(self):
            return self.XP
        def getNXTLVL(self):
            return self.NXTLVL
        def __str__(self):
            print"""
Name: %s

Level:   %s
XP:      %s
Nxt Lvl: %s

Location: %s

HP: %s

ST: %s
PE: %s
EN: %s
CH: %s
IN: %s
AG: %s
LK: %s

Small Guns:    %s
Big Guns:      %s
Unarmed:       %s
Melee Weapons: %s
Throwing:      %s
First-Aid:     %s
Sneak:         %s
Lockpick:      %s
Steal:         %s
Science:       %s
Repair:        %s
Speech:        %s
""" %(self.getname(), self.getlevel(), self.getXP(), self.getNXTLVL(), self.getlocation(), self.getHP(), self.getST(), self.getPE(), self.getEN(), self.getCH(), self.getIN(), self.getAG(), self.getLK(), self.getsmallguns(), self.getbigguns(), self.getunarmed(), self.getmeleeweapons(), self.getthrowing(), self.getfirstaid(), self.getsneak(), self.getlockpick(), self.getsteal(), self.getscience(), self.getrepair(), self.getspeech())

player_location = {'player_grid_height': 1, 'player_grid_length': 1}

playercharacter = player(raw_input("""What is your name?
"""), 1, 5, 5, 5, 5, 5, 5, 5, player_location, 1, 0, 50)

playercharacter.__str__()

此外,任何一般性建议都很好,谢谢!

1 个答案:

答案 0 :(得分:2)

您过度缩进了其他方法;所有def get*名称的缩进级别应与def __init__相同,而不是__init__的内容级别。

就一般建议而言:不要编写Java风格的访问器。 Python的方法是直接访问属性,或者如果您坚持某种保护,则使用前导下划线命名实例属性,并使用@property访问器。因此,如果name应该受到保护以免发生突变,那么您将:

def __init__(self, ...):
    self._name = ...

@property
def name(self):
    return self._name

因此该属性的使用仍然为myobj.name,而无需显式调用函数或使用多余的get前缀。