我想计算在类FUNCTION
中执行一个特定功能CLASS.
的次数,我试图通过在类counting_function_execution
中定义一个全局变量并增加它来增加次数每次执行该函数:
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
答案 0 :(得分:3)
问题在于,在函数CLASS.FUNCTION
中,您不是在引用类变量counting_function_execution
,而是在引用另一个具有相同名称的局部变量。
这就是为什么“在分配之前先引用”错误的原因,因为在初始化之前先引用了该局部变量。
要引用正确的变量,可以使用self.counting_function_execution
(实例的变量)或CLASS.counting_function_execution
(类的静态变量)
答案 1 :(得分:1)
counting_function_execution
是一个类变量。如果要从类的实例访问它,请使用self.counting_function_execution
。如果您想从类本身访问它,请使用x.counting_function_execution
,其中x
是类的名称(在这种情况下为CLASS
)
两种方式均可用于您的示例
答案 2 :(得分:1)
Henry,您在这里得到了一些令人困惑的答案,因为它并不是100%清楚您想要什么,尤其是在您有多个类实例的情况下。如果要对所有变量进行一次计数,则需要从实例中引用类变量,例如:
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。
如果您改为使用self.counting_function_execution += 1
,则每个实例将获得其自己的计数,而CLASS.counting_function_execution
的末尾将为零,因为您从未实际对其进行递增。
无论哪种方式,您都可以避免使用global
变量,这是一个奖励。
答案 3 :(得分:0)
如果要计算每个类的执行量,则可以在类之外定义一些内容:
counting_function_execution = 0
def count_func(func):
def decorator_count(self, *args, **kwargs):
global counting_function_execution
counting_function_execution += 1
print(counting_function_execution)
return func(self, *args, **kwargs)
return decorator_count
class CLASS:
# global counting_function_execution
# counting_function_execution = 0
def __init__(self,name):
self.name = name
@count_func
def FUNCTION(self):
print("Hi " + self.name)
#print("This function was already excecuted ", self.counting_function_execution, " number of times.")
如果您要计算每个实例的执行量,则只需在引用它时添加self
,即self.counting_function_execution
。
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 ", self.counting_function_execution, " number of times.")
self.counting_function_execution += 1