Why the following counter does not work, and how to fix it:
class Test():
   def __init__(self):
       self.counter = 0
   def increment(self):
       print counter++
Thank you.
Why the following counter does not work, and how to fix it:
class Test():
   def __init__(self):
       self.counter = 0
   def increment(self):
       print counter++
Thank you.
 
    
    to use a variable in a class, preface it with self.
class MyCounter():
   def __init__(self, number):
       self.counter = number
   def increment(self):
       self.counter += 1
       print(self.counter)
[IN] >>> counter = MyCounter(5)
[IN] >>> print(counter.increment())
[OUT] >>> 6
 
    
    you can use__init__
class C:
    counter = 0
    def __init__(self):
        C.counter += 1
or
class C:
    counter = 0
    @classmethod
    def __init__(cls):
        cls.counter += 1
or use another method like
class C:
    counter = 0
    @classmethod
    def my_function(cls):
        cls.counter += 1
