计算一个类实例的另一个函数的执行后,一个函数被执行多少次 [英] Count How Many Times a Function is Excuted after the Exceution of Another Function for an Instance of a Class

查看:61
本文介绍了计算一个类实例的另一个函数的执行后,一个函数被执行多少次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于这个问题,我知道如何计算函数函数在类 CLASS 。现在,我还有另一个问题:我如何计算第二个函数 FUNCTION2 CLASS 的一个实例)多少次?在执行第一个函数 FUNCTION 之后执行$ c>吗?

Based on this question, I know how to count how many times a function FUNCTION was executed within an instance of a class CLASS. Now, I have another question: How would I count how many times (for one instance of the class CLASS) a second function FUNCTION2 is executed after the first function FUNCTION is executed ?

这是我尝试过的一个小例子它:

Here is a small example of how I tried it:

class CLASS:
    # Initializing counting for first function
    counting_function_execution  = 0

    def __init__(self,name):
        self.name = name

    def FUNCTION1(self):
        # Initializing counting for second function
        counting_excution_after_FUNCTION = 0

        self.counting_function_execution += 1
        print("FUNCTION 1 was excecuted ", self.counting_function_execution, " time.")

    def FUNCTION2(self):
        counting_excution_after_FUNCTION += 1
        print("FUNCTION 2 was excecuted ", self.counting_excution_after_FUNCTION, " time after FUNCTION.")

...但是我得到了:

...but I got:

test = CLASS("Fred")
test.FUNCTION1()
test.FUNCTION2()
test.FUNCTION2()

输出:

FUNCTION 1 was excecuted  1  time.
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-148-d67aeefa671e> in <module>()
      1 test = CLASS("Fred")
      2 test.FUNCTION1()
----> 3 test.FUNCTION2()
      4 test.FUNCTION2()

<ipython-input-147-6a6d7adb1af9> in FUNCTION2(self)
     15 
     16     def FUNCTION2(self):
---> 17         counting_excution_after_FUNCTION += 1
     18         print("FUNCTION 2 was excecuted ", self.counting_excution_after_FUNCTION, " time after FUNCTION.")

UnboundLocalError: local variable 'counting_excution_after_FUNCTION' referenced before assignment


推荐答案

如果您跟踪 FUNCTION1 被调用,您可以在 FUNCTION2 中进行测试,并确保它大于零,然后再开始计数 FUNCTION2

If you keep track of how many times FUNCTION1 is called, you can test for that in FUNCTION2 and make sure it's above zero before you start counting FUNCTION2:

class CLASS:

    def __init__(self,name):
        self.name = name
        # initialize instance counters -- each instance gets its own counts
        self.counting_function_execution_1 = 0
        self.counting_function_execution_2 = 0

    def FUNCTION1(self):
        self.counting_function_execution_1 += 1
        print("FUNCTION 1 was excecuted ", self.counting_function_execution_1, " number of times.")

    def FUNCTION2(self):
        if self.counting_function_execution_1:     # don't count unless function1 has run
            self.counting_function_execution_2 += 1
        print("FUNCTION 2 was excecuted ", self.counting_function_execution_2, " number of times after FUNCTION.")

c = CLASS('the dude')
c.FUNCTION2() #0
c.FUNCTION2() #0
c.FUNCTION1()

c.FUNCTION2() #1
c.FUNCTION2() #2

这篇关于计算一个类实例的另一个函数的执行后,一个函数被执行多少次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆