如何在python中重置类实例?

时间:2016-10-11 22:10:59

标签: python-3.x attributes instance

我正在尝试返回斐波纳契数字,但是在调用start.next()。next()之后它会打印2而不是1。我该如何解决这个问题?

class Fib():

    def __init__(self):
        self.value = 0



    def next(self):

        if self.value == 0:
            self.total = 1
            self.previous = 0
            self.value = 1

        else:
            print(self.previous)
            self.value = self.total + self.previous
            hold = self.total
            self.previous, self.total = hold, self.value


        return self

    def __repr__(self):
        return str(self.value)

0 个答案:

没有答案