计算执行某类功能的次数 [英] Counting Number of Times a Function of a Class is Excuted

查看:55
本文介绍了计算执行某类功能的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算在类 CLASS中执行特定功能功能的次数。为此,可以在 counting_function_execution 类中定义一个全局变量,并在每次执行该函数时增加它:

I would like to count how many times I execute a specific function FUNCTION in a class CLASS. I tried to do it by definig a global variable within the class counting_function_execution and increasing it every time the function is executed:

class CLASS:
    global counting_function_execution 
    counting_function_execution  = 0

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

    def FUNCTION(self):
        print("Hi " + self.name)
        print("This function was already excecuted ", counting_function_execution, " number of times.")
        counting_function_execution += 1

但是,当我这样做时:

test = CLASS("Fred")
test.FUNCTION()
test.FUNCTION()

我得到:

---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-104-dc8f3ae96700> in <module>()
      1 test = CLASS("Fred")
----> 2 test.say_hi()

<ipython-input-100-d0166079c633> in say_hi(self)
      8     def say_hi(self):
      9         print("Hi " + self.name)
---> 10         print("This function was already excecuted ", counting_function_execution, " number of times.")
     11         counting_function_execution += 1
     12 

UnboundLocalError: local variable 'counting_function_execution' referenced before assignment


推荐答案

Henry,您会得到一些令人困惑的答案在这里,因为不是100%清楚您想要什么,特别是如果您有该类的多个实例。如果要对所有变量进行一次计数,则需要从实例中引用类变量:

Henry, you're getting some confusing answers here because it's not 100% clear what you want especially if you have multiple instances of the class. If you want ONE count for all variables, you need to reference the class variable from the instance like:

class CLASS:
    # static class counting_function_execution 
    counting_function_execution  = 0

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

    def FUNCTION(self):
        print("Hi " + self.name)
        print("This function was already excecuted ", self.counting_function_execution, " number of times.")
        self.__class__.counting_function_execution += 1

c = CLASS("mark")
c.FUNCTION()      # 0
c.FUNCTION()      # 1

d = CLASS("john")
d.FUNCTION()      # 2
d.FUNCTION()      # 3

print(CLASS.counting_function_execution) #4

这将打印0-3,最后 CLASS.counting_function_execution 等于4。

This will print 0 - 3 and at the end CLASS.counting_function_execution will equal 4.

如果您改用 self.counting_function_execution + = 1 每个实例将获得其自己的计数,并且 CLASS.counting_function_execution 最后将为零,因为您从未实际对其进行递增。

If you instead use self.counting_function_execution += 1 each instance will get its own count and CLASS.counting_function_execution will be zero at the end because you never actually increment it.

无论哪种方式,都可以避免 global 变量,这是一个奖励。

Either way you can avoid the global variable which is a bonus.

这篇关于计算执行某类功能的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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