1

いくつかのクラス変数を持ち、それらの変数に対して何かを実行する関数を持つクラスが必要ですが、関数が自動的に呼び出されるようにしたいです。それを行うより良い方法はありますか?これにはinitを使用する必要がありますか?これが初心者の質問である場合は申し訳ありません-私はPythonにまったく慣れていません。

# used in second part of my question
counter = 0    

class myClass:
    foo1 = []
    foo2 = []

    def bar1(self, counter):
        self.foo1.append(counter)
    def bar2(self):
        self.foo2.append("B")

def start():
    # create an instance of the class
    obj = myClass()
    # I want the class methods to be called automatically...
    obj.bar1()
    obj.bar2()

# now what I am trying to do here is create many instances of my class, the problem is
# not that the instances are not created, but all instances have the same values in 
# foo1 (the counter in this case should be getting incremented and then added
while(counter < 5):
    start()
    counter += 1

これを行うためのより良い方法はありますか?そして、すべてのオブジェクトが同じ値を持つようになっていますか? ありがとう!

4

1 に答える 1