oop - Creating instances of classes python & __init__ -


i want class has class variables, , has functions perform stuff on variables - want functions called automatically. there better way it? should using init this? sorry if nooby question - quite new python.

# used in second part of 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 instance of class     obj = myclass()     # want class methods called automatically...     obj.bar1()     obj.bar2()  # trying here create many instances of class, problem # not instances not created, instances have same values in  # foo1 (the counter in case should getting incremented , added while(counter < 5):     start()     counter += 1 

so there better way this? , causing of objects have same values? thanks!

foo1 , foo2 class variables, shared objects,

your class should this, if want foo1, foo2 different every object:

class myclass:     # __init__ function initialize `foo1 & foo2` every object     def __init__(self):         self.foo1 = []         self.foo2 = []      def bar1(self, counter):         self.foo1.append(counter)     def bar2(self):         self.foo2.append("b") 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -