'NoneType'对象没有属性

时间:2016-05-16 21:55:26

标签: python attributes project

我收到有关'NoneType' object has no attribute...的错误消息。我该如何解决?

def fight(self, pMonstername):
    print ("The fight begin")
    MonInList = self.monsterSearch(pMonstername) 
#at first I have the method "Fight" where i build a temp variable MonInList
#which I declare with the help of the methode "monsterSearch" 

def monsterSearch(self, pMonstername):
    for m in self.monsterList:
        if m.name == pMonstername:
            return m
#in this method I go through step by step through the monsterList, in which 
#are objects of type Monster are stored and pick out those monster which has
#the same name as those I search for and this object should be returned

if self.player.getStrength() > MonInListe.getStrength:
#later I want to compare the strength of the player and the Monster, which i 
#declared before, but then there is the error:
 AttributeError: 'NoneType' object has no attribute 'getStrength'

2 个答案:

答案 0 :(得分:0)

简答

这个问题的简短回答是,错误AttributeError: 'NoneType' object has no attribute 'getStrength'表示您在包含条件self.player = None的类的__init__()中有if self.player.getStrength() > ...,但它不是&{ #39;在条件之前正确更新为其他东西(可能是类Player的实例?)。

还会引发一些其他问题和错误,但这是我开始尝试调试的地方。

长答案

另一个需要注意的重要事项是,仅仅因为您在课堂上包含该方法并不意味着它会自动运行。您遇到了您所描述的错误,而不是我认为在您的代码中看到的其他错误之一,这表明这些方法要么没有以正确的顺序调用,要么在比较中正确使用,或者根本没有被召唤。这就是为什么我认为Player类没有被正确调用。

举一个例子,请参阅下面的代码块。它是问题中提供的内容的变体,如果它运行,它将重现问题。首先是类和方法:

# The post author mentioned there objects of the type Monster
# stored in a list in the main class. 
# Using the main monster characters from Monsters Inc, 
# below are hastily constructed classes to attempt to reproduce the issue.
class Sully(object):

    def __init__(self):
        self.name = "James P. Sullivan"
        self.getStrength = 10



class Player(object):

    def getStrength(self):
        return 100


class SomeClass(object):

    def __init__(self):
        self.monsterList = [Sully]
        self.player = None


    def fight(self, pMonstername):
        #at first I have the method "Fight" where i build a temp variable MonInList
        #which I declare with the help of the methode "monsterSearch" 
        print ("The fight begin")
        MonInList = self.monsterSearch(pMonstername) 

    def monsterSearch(self, pMonstername):
        #in this method I go through step by step through the monsterList, in which 
        #are objects of type Monster are stored and pick out those monster which has
        #the same name as those I search for and this object should be returned
        for m in self.monsterList:
            if m.name == pMonstername:
                return m

    def compareStrength(self):

        #later I want to compare the strength of the player and the Monster, which i 
        #declared before, but then there is the error:
        if self.player.getStrength() > MonInListe.getStrength:
            print("Success!")
            # AttributeError: 'NoneType' object has no attribute 'getStrength'

接下来,让我们创建一个SomeClass实例,然后运行compareStrength方法:

s = SomeClass()
s.compareStrength()

结果是这个错误,堆栈提供完整:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-71-bfa22232a4a7> in <module>()
      1 s = SomeClass()
----> 2 s.compareStrength()

<ipython-input-70-3a4fe2cd3c3c> in compareStrength(self)
     42         #later I want to compare the strength of the player and the Monster, which i
     43         #declared before, but then there is the error:
---> 44         if self.player.getStrength() > MonInListe.getStrength:
     45             print("Success!")
     46             # AttributeError: 'NoneType' object has no attribute 'getStrength'

AttributeError: 'NoneType' object has no attribute 'getStrength'

有趣的是,这不应该是触发的第一个错误。我本来期望这个错误:

The fight begin
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-79-5009b509291a> in <module>()
      1 s = SomeClass()
----> 2 s.fight("James P. Sullivan")
      3 s.compareStrength()

<ipython-input-77-13d18f4abe98> in fight(self, pMonstername)
     29         #which I declare with the help of the methode "monsterSearch"
     30         print ("The fight begin")
---> 31         MonInList = self.monsterSearch(pMonstername)
     32 
     33     def monsterSearch(self, pMonstername):

<ipython-input-77-13d18f4abe98> in monsterSearch(self, pMonstername)
     36         #the same name as those I search for and this object should be returned
     37         for m in self.monsterList:
---> 38             if m.name == pMonstername:
     39                 return m
     40 

AttributeError: type object 'Sully' has no attribute 'name'

...这是由运行此代码引起的:

s = SomeClass()
s.fight("James P. Sullivan")
s.compareStrength()

我认为应该调用s.fight()并触发此错误,否则MonInList在运行compareStrength()时不正确。由于这个原因,我认为只是简单地查看方法被调用的顺序将解决问题中的主要问题,这就是为什么我把它包含在这里(尽管看起来似乎是切向的)。

哦,顺便说一句,所描述的最新问题的解决方法是将行if m.name == pMonstername:更改为if m().name == pMonstername:

希望有所帮助!

答案 1 :(得分:-1)

好的,所以看起来您的对象'MonInListe'在使用之前尚未实例化。然后它变成'无'并且实际上没有属性。 您应该检查代码中的其他位置(不在此处显示的部分),以便正确实例化此对象“MonInListe”。 您遇到的错误的来源绝对不会包含在您在此处公开的代码部分中。