首页 > 解决方案 > python:在乘法运行方法中使用类变量作为计数器

问题描述

可以说我有以下简化的python代码:

class GUI:
    def __init__(self):
        self.counter = 0
        self.f1c = 0
        self.f2c = 0
    
    def update(self):
        self.counter = self.counter + self.f1()
        self.counter = self.counter + self.f4()
        self.counter = self.counter + self.f2()
        self.counter = self.counter + self.f3()

        print(self.counter)
        if (self.counter > 4):
            print("doing update now")
            # do_update()
            self.counter = 0
    
    def f1(self):
        if self.f1c < 2:
            self.f1c = self.f1c + 1
            self.update()
        return 1

    def f2(self):
        if self.f2c < 4:
            self.f2c = self.f2c + 1
            self.update()
        return 0

    def f3(self):
        return 1

    def f4(self):
        return 0


g = GUI()
g.update()

在我的情况下,测试运行的示例输出是:

6
doing update now
5
doing update now
4
3
2
2
2

虽然我希望它是:

1
2
3
4
5
doing update now
0
1

据我了解,在我的代码示例中甚至不可能self.counter从不做.21do_update()

这样做的正确方法是什么?

编辑:基本上,我想要的是,do_update只有在所有其他功能结束后才会运行AND counter > 0

标签: pythonrecursion

解决方案


您在这里进行了一些非常复杂的递归,老实说,我认为您很幸运没有创建无限循环。

为了查看您所拥有的内容发生了什么,我使用调试器打开了您的代码,并对调用进行了注释,只是为了跟踪前几个打印语句的流程顺序。增加缩进表示更深的嵌套函数调用,然后列出带有行本身的行号。注释指示每个阶段某些变量的当前值,并指示打印语句何时发生:

--Call-- 39| g.update()
    --Call-- 08| self.counter = self.counter + self.f1() #f1c is 0
        --Call-- 22| self.update() #f1c is 1
            --Call-- 08| self.counter = self.counter + self.f1() #f1c is 1
                --Call-- 22| self.update() #f1c is 2
                    --Call-- 08| self.counter = self.counter + self.f1() #f1c is 2
                        --Return-- 23| return 1 #self.counter now == 1
                    --Call-- 09| self.counter = self.counter + self.f4()
                        --Return-- 35| return 0 #self.counter now == 1
                    --Call-- 10| self.counter = self.counter + self.f2() #f2c is 0
                        --Call-- 28| self.update() #f2c is 1
                            --Call-- 08| self.counter = self.counter + self.f1() #f1c is 2
                                --Return-- 23| return 1 #self.counter now == 2
                            --Call-- 09| self.counter = self.counter + self.f4()
                                --Return-- 35| return 0 #self.counter now == 2
                            --Call-- 10| self.counter = self.counter + self.f2() #f2c is 1
                                --Call-- 28| self.update() #f2c is 2
                                    --Call-- 08| self.counter = self.counter + self.f1() #f1c is 2
                                        --Return-- 23| return 1 #self.counter now == 3
                                    --Call-- 09| self.counter = self.counter + self.f4()
                                        --Return-- 35| return 0 #self.counter now == 3
                                    --Call-- 10| self.counter = self.counter + self.f2() #f2c is 2
                                        --Call-- 28| self.update() #f2c is 3
                                            --Call-- 08| self.counter = self.counter + self.f1() #f1c is 2
                                                --Return-- 23| return 1 #self.counter now == 4
                                            --Call-- 09| self.counter = self.counter + self.f4()
                                                --Return-- 35| return 0 #self.counter now == 4
                                            --Call-- 10| self.counter = self.counter + self.f2() #f2c is 3
                                                --Call-- 28| self.update() #f2c is 4
                                                    --Call-- 08| self.counter = self.counter + self.f1() #f1c is 2
                                                        --Return-- 23| return 1 #self.counter now == 5
                                                    --Call-- 09| self.counter = self.counter + self.f4()
                                                        --Return-- 35| return 0 #self.counter now == 5
                                                    --Call-- 10| self.counter = self.counter + self.f2() #f2c is 4
                                                        --Return-- 29| return 0 #self.counter now == 5
                                                    --Call-- 11| self.counter = self.counter + self.f3()
                                                        --Return-- 32| return 1 #self.counter now == 6
                                                    ### print(self.counter) ### #self.counter now == 6
                                                    ### print('doing update')
                                                    #self.counter now == 0
                                                    --Return-- 18| #self.counter in prior stack frame was only 4
                                                --Return-- 29| return 0 #self.counter now == 4
                                            --Call-- 11| self.counter = self.counter + self.f3()
                                                --Return-- 32| return 1 #self.counter now == 5
                                            ### print(self.counter) ### #self.counter now == 5
                                            ### print('doing update')
                                            #self.counter now == 0
                                            --Return-- 18| #self.counter in prior stack frame was only 3

推荐阅读