如何修复错误:“AttributeError:'str'对象没有属性'make_lab'”?

时间:2016-12-25 17:17:43

标签: python string class

这是我的代码:

class Student():
    exam_max=None
    lab_max=None
    lab_num=None
    k=None
    students=[]
    labs=None
    exam=None

    def __init__(self, name=None, conf=None):
        keys = conf.keys()
        self.name=name
        self.students.append({"name":self.name, "labs":[], "exam":0})
        if 'exam_max' in keys:
            self.exam_max = conf['exam_max']
        if 'lab_max' in keys:
            self.lab_max = conf['lab_max']
        if 'lab_num' in keys:
            self.lab_num = conf['lab_num']
        if 'k' in keys:
            self.k = conf['k']

    def make_lab(self, m=None, n=None):
        self.m = m
        self.n = n
        for i in self.students:
            for k,v in i.iteritems():
                if v == self.name:
                    if len(i.get("labs"))<self.lab_num:
                        while len(i.get("labs"))<self.lab_num:
                            i.get("labs").append(0)
        if self.n==None:
            for i in self.students:
                for k,v in i.iteritems():
                    if v == self.name:
                        for b in range(len(i.get("labs"))):
                            if i.get("labs")[b]==0:
                                if self.m>=self.lab_max:
                                    i.get("labs")[b]=self.lab_max
                                    break
                                else:
                                    i.get("labs")[b]=self.m
                                    break
        elif self.n>=0 and self.n<=self.lab_num-1:
            for i in self.students:
                for k,v in i.iteritems():
                    if v == self.name:
                        for b in range(len(i.get("labs"))):
                            if b==self.n:
                                if self.m>=self.lab_max:
                                    i.get("labs")[b]=self.lab_max
                                else:
                                    i.get("labs")[b]=self.m
        for i in self.students:
            for k,v in i.iteritems():
                if v == self.name:
                    labs=i.get("labs")
                    exam=i.get("exam")    
        return "labs: {}, exam: {}".format(" ".join(str(v) for v in labs), str(exam))                    

    def make_exam(self, m=0):
        self.m_e=m

        for i in self.students:
            for k,v in i.iteritems():
                if v == self.name:
                    if self.m_e>=self.exam_max:
                        i["exam"]=self.exam_max
                    else:
                        i["exam"]=self.m_e

        for i in self.students:
            for k,v in i.iteritems():
                if v == self.name:
                    labs=i.get("labs")
                    exam=i.get("exam")    
        return "labs: {}, exam: {}".format(" ".join(str(v) for v in labs), str(exam))

    def is_certified(self):
        for i in self.students:
            for k,v in i.iteritems():
                if v == self.name:
                    rez=sum(i.get("labs"))+i.get("exam")

            return (rez, (rez/100)>=self.k,)

        return None

如果方法调用是在一个上进行的,则一切正常:

>>> oleg.make_lab(1)
'labs: 1 0 0 0 0 0 0 0 0 0, exam: 0'

可是:

>>> oleg.make_lab(1).make_lab(8,0).make_lab(1).make_lab(10,7).make_lab(4,1).make_lab(5).make_lab(6.5).make_exam(32)

Traceback (most recent call last):
  File "<pyshell#40>", line 1, in <module>
oleg.make_lab(1).make_lab(8,0).make_lab(1).make_lab(10,7).make_lab(4,1).make_lab(5).make_lab(6.5).make_exam(32)
AttributeError: 'str' object has no attribute 'make_lab'

2 个答案:

答案 0 :(得分:0)

oleg的类型为Student,因此它的方法为make_lab。 方法make_lab返回一个字符串,而不是Student类型 - 因此它没有make_lab方法。

答案 1 :(得分:0)

方法make_lab()返回一个字符串值,我们可以在方法声明中观察到。你正在努力实现的是一种经典的 方法链接
在你的情况下,make_lab()方法的第一次调用(在oleg.make_lab(1).make_lab(8,0).....内)将返回一个字符串,但第二次连续调用会自然地抛出AttributeError: 'str' object has no attribute 'make_lab'。构建一系列方法调用 - 链中的所有方法都必须返回self(当前实例)。

...

def make_lab(self, m=None, n=None):
    ...
    print("labs: {}, exam: {}".format(" ".join(str(v) for v in labs), str(exam)))
    return self

https://en.wikipedia.org/wiki/Method_chaining