国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

python - 程序運行會出現(xiàn)錯誤
過去多啦不再A夢
過去多啦不再A夢 2017-06-22 11:52:35
0
1
893
class Person(object):
    def __init__(self,name):
        self.name = name
class Teacher(Person):
    def __init__(self,score):
        self.__score = score
class Student(Teacher,Person):
    def __init__(self,name,score):
        Person.__init__(self,name)
        super(Student,self).__init__(score)
    @property
    def score(self):
        return self.__score
    @score.setter
    def score(self,score):
        if score<0 or score >100:
            raise ValueError('invalid score')
        self.__score = score
    def __str__(self):
        return 'Student:%s,%d' %(self.name,self.score)

s1 = Student('Jack',89)
s1.score = 95
print s1

在運行這個程序時,只有當(dāng)score是私有變量的時候才能正常運行,是property的某些特性嗎,還是什么?如果只設(shè)置為self.score = score,就會出現(xiàn)‘maximum recursion depth exceeded while calling a Python object’的錯誤,求大神解答

過去多啦不再A夢
過去多啦不再A夢

全部回復(fù)(1)
洪濤

會產(chǎn)生這個困惑的原因是對python的getter裝飾器和setter裝飾器不夠熟悉

當(dāng)你聲明了對score屬性的setter裝飾器之后, 實際上對這個score進行賦值就是調(diào)用這個setter裝飾器綁定的方法

所以你的setter要訪問的成員變量不能和setter方法同名, 不然就相當(dāng)于一個無盡的迭代:

self.score(self.score(self.score(self.score(self.score........ 無盡的迭代,

當(dāng)然會報超過最大迭代深度的錯誤了

最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板