Python 当其中一个条件发生变化时,如何重置计数器(全局变量)
当其中一个条件发生变化时,如何重置计数器(全局变量)。基本上,将计数器增加1,直到“type_name”变为“Bar”,如果它变为其他值,请将计数器设置为0。如何为多个类型名称实现这一点。多谢各位Python 当其中一个条件发生变化时,如何重置计数器(全局变量),python,class,Python,Class,当其中一个条件发生变化时,如何重置计数器(全局变量)。基本上,将计数器增加1,直到“type_name”变为“Bar”,如果它变为其他值,请将计数器设置为0。如何为多个类型名称实现这一点。多谢各位 type_name = "bar" counter = 0 class Foo(object): """ if type is different in the argument, reset counter to 0. Otherwise, increment the c
type_name = "bar"
counter = 0
class Foo(object):
"""
if type is different in the argument, reset counter to 0.
Otherwise, increment the counter by 1.
"""
def __init__(self, type_name):
self.type_name = type_name
self.counter = counter
self.reset()
def increament_counter(self):
self.counter +=1
def reset(self):
if self.type_name != type_name:
self.counter = 0
b = Foo("bar")
b.increament_counter()
b.increament_counter()
print b.counter
print "==============================="
c = Foo("taste")
c.reset()
print c.counter
c.increament_counter()
print c.counter
print "--------------------------"
d = Foo("bar")
d.increament_counter()
d.increament_counter()
print d.counter
print "--------------------------"
e = Foo("car")
e.increament_counter()
e.increament_counter()
print e.counter
type_name = "bar"
counter = 1
class Foo(object):
"""
if type is different in the argument, reset counter to 0.
Otherwise increament counter by 1.
"""
def __init__(self, type_name):
self.type_name = type_name
self.counter = counter
self.reset()
def reset(self):
global counter
if self.type_name != type_name:
counter = 1
else:
counter += 1
c = Foo("taste")
print c.counter
print "--------------------------"
d = Foo("bar")
print d.counter
print "--------------------------"
e = Foo("taste")
print e.counter
print "--------------------------"
new_instances = []
for i in range(0, 10):
new_instances.append(Foo("bar"))
print new_instances
print new_instances[0].counter
print new_instances[8].counter
这里要做的不是修改全局变量,而是修改类中的计数器(self.counter)
您需要做的是,在重置函数中,更改全局计数,而不是类对象属性中的全局计数。
def reset(self):
global counter
if self.type_name != type_name:
counter = 0
type_name = "bar"
counter = 1
class Foo(object):
"""
if type is different in the argument, reset counter to 0.
Otherwise increament counter by 1.
"""
def __init__(self, type_name):
self.type_name = type_name
self.counter = counter
self.reset()
def reset(self):
global counter
if self.type_name != type_name:
counter = 1
else:
counter += 1
c = Foo("taste")
print c.counter
print "--------------------------"
d = Foo("bar")
print d.counter
print "--------------------------"
e = Foo("taste")
print e.counter
print "--------------------------"
new_instances = []
for i in range(0, 10):
new_instances.append(Foo("bar"))
print new_instances
print new_instances[0].counter
print new_instances[8].counter
这里要做的不是修改全局变量,而是修改类中的计数器(self.counter)
您需要做的是,在重置函数中,更改全局计数,而不是类对象属性中的全局计数。
def reset(self):
global counter
if self.type_name != type_name:
counter = 0
type_name = "bar"
counter = 1
class Foo(object):
"""
if type is different in the argument, reset counter to 0.
Otherwise increament counter by 1.
"""
def __init__(self, type_name):
self.type_name = type_name
self.counter = counter
self.reset()
def reset(self):
global counter
if self.type_name != type_name:
counter = 1
else:
counter += 1
c = Foo("taste")
print c.counter
print "--------------------------"
d = Foo("bar")
print d.counter
print "--------------------------"
e = Foo("taste")
print e.counter
print "--------------------------"
new_instances = []
for i in range(0, 10):
new_instances.append(Foo("bar"))
print new_instances
print new_instances[0].counter
print new_instances[8].counter
多谢各位。由于篇幅的限制,我在这里添加了更新的代码,还有一个用例
type_name = "bar"
counter = 1
class Foo(object):
"""
if type is different in the argument, reset counter to 0.
Otherwise increament counter by 1.
"""
def __init__(self, type_name):
self.type_name = type_name
self.counter = counter
self.reset()
def reset(self):
global counter
if self.type_name != type_name:
counter = 1
else:
counter += 1
c = Foo("taste")
print c.counter
print "--------------------------"
d = Foo("bar")
print d.counter
print "--------------------------"
e = Foo("taste")
print e.counter
print "--------------------------"
new_instances = []
for i in range(0, 10):
new_instances.append(Foo("bar"))
print new_instances
print new_instances[0].counter
print new_instances[8].counter
多谢各位。由于篇幅的限制,我在这里添加了更新的代码,还有一个用例
type_name = "bar"
counter = 1
class Foo(object):
"""
if type is different in the argument, reset counter to 0.
Otherwise increament counter by 1.
"""
def __init__(self, type_name):
self.type_name = type_name
self.counter = counter
self.reset()
def reset(self):
global counter
if self.type_name != type_name:
counter = 1
else:
counter += 1
c = Foo("taste")
print c.counter
print "--------------------------"
d = Foo("bar")
print d.counter
print "--------------------------"
e = Foo("taste")
print e.counter
print "--------------------------"
new_instances = []
for i in range(0, 10):
new_instances.append(Foo("bar"))
print new_instances
print new_instances[0].counter
print new_instances[8].counter
(在需要访问全局变量的地方以类似的方式进行修改)谢谢。我更新了代码,此修改满足以下用例。我很高兴。:)(在需要访问全局变量的地方以类似的方式进行修改)谢谢。我更新了代码,此修改满足以下用例。我很高兴。:)我刚刚意识到我不应该使用递增计数器。我刚刚意识到我不应该使用递增计数器。