Python:TypeError:unbound方法,必须使用(class)实例调用

时间:2016-07-31 02:14:12

标签: python

有人可以帮我理解这个错误吗?在我的班级“生物”中,我有这个:

def mate_action(self):
    if self.mate == 1:
        for creature in creature_list:
            if creature.mate == 1:
                self.str_nb = (self.str + creature.str) / 2
                self.attr_nb = (self.attr + creature.attr) / 2
                self.cons_nb = (self.cons + creature.cons) / 2
                self.size_nb = (self.size + creature.size) / 2
                creature.mate = 0
                creature_list.append(Creature)
                for creature in creature_list:
                    if creature.alive == 0:
                        creature.alive = 1
                        creature.str = self.str_nb
                        creature.attr = self.attr_nb
                        creature.cons = self.cons_nb
                        creature.size = self.size_nb
                        creature.nb = 1

当我这样做时:

for creature in creature_list:  
    creature.mate_action()

我收到此错误:

TypeError: unbound method mate_action() must be called with Creature instance as first argument (got nothing instead)

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

@zondo给出了答案。我需要将creature_list.append(Creature)更改为creature_list.append(Creature())