0
タイプ名が同じ場合は1をインクリメントし、それ以外の場合は1にリセットします。カスタムスタートレンジも追加します。ここで問題となるのは、開始範囲を1回追加してから、1ずつインクリメントし、毎回start_range + 1ずつ増加させないことです。タイプが同じ場合はインクリメントカウンタを、それ以外の場合は指定された開始レンジにリセット
class Foo(object):
"""
if type is different in the argument, reset counter to 1.
Otherwise increment counter by 1. Use start_range as a starting point.
"""
type_name = "bar"
counter = 1
def __init__(self, next_type_name, start_range):
self.next_type_name = next_type_name
self.start_range = start_range
self.reset()
def reset(self):
Foo.counter =self.start_range
print self.next_type_name, Foo.type_name
if Foo.type_name != self.next_type_name:
print "here..."
Foo.counter = self.start_range+1
Foo.type_name = self.next_type_name
else:
Foo.counter += 1
print Foo.counter
print "-------------------------- bar "
d = Foo("bar", 100)
print d.counter
## result >> 101
print "-------------------------- list "
new_instances = []
for i in range(0, 10):
new_instances.append(Foo("bar", 100))
print new_instances[i].counter
## result >> 102, 103, 104, 105, 106, 107, 108, 109, 110, 111
print "-------------------------- test starts here ... "
c = Foo("test", 200)
print c.counter
## result >> 201
print "-------------------------- test "
e = Foo("test", 200)
print e.counter
## result >> 202
print "-------------------------- test "
f = Foo("test", 200)
print f.counter
## result >> 203
これは非常にきちんとしています。ありがとうございました。 – June2017
@ June2017こんにちは^ _ ^ – Fogmoon