2

I want to have a counter which increments every time a subclass is instantiated. How would I achieve this such that the last statement below evaluates to True:

class Abstract(ABC):
    counter = 0

class A(Abstract):
    pass
class B(Abstract):
    pass

a = A()
b = B()
a.counter += 1
b.counter == 1

Currently each subclass gets its own counter, rather than sharing the one outlined in the superclass.

cs95
  • 379,657
  • 97
  • 704
  • 746
Alesi Rowland
  • 379
  • 2
  • 16
  • 1
    *"each subclass gets its own counter "* - no, `a.counter += 1` is not updating the `A` `counter` *or* the `Abstract` `counter`. Assigning to instance attributes assigns them *on the instance*. See e.g. https://stackoverflow.com/q/68645/3001761. – jonrsharpe Oct 27 '20 at 10:18
  • This is True, in reality my code was calling cls.counter += 1 however. – Alesi Rowland Oct 27 '20 at 12:50

2 Answers2

2

Would this work for you?

global_counter = 0


class Abstract:
    def __init__(self):
        global global_counter
        global_counter += 1


class A(Abstract):
    def __init__(self):
        super().__init__()


class B(Abstract):
    def __init__(self):
        super().__init__()


a = A()
b = B()

print(global_counter)    # (output: 2)
nihilok
  • 1,325
  • 8
  • 12
  • It would work however I tend to avoid using global (probably due to articles I've read on avoiding it) – Alesi Rowland Oct 26 '20 at 12:03
  • Entirely depends how you use em. The trouble is every time you instantiate the class with the counter you instantiate the counter as 0 – nihilok Oct 26 '20 at 12:24
  • Yep, for some reason i thought subclasses share the same instance of super class attributes. Turns out they do but only for not base types I think. Building a "Counter" class which is effectively a fancy integer also works! – Alesi Rowland Oct 27 '20 at 10:10
0

So I've implemented several different ways to achieve what I wanted:

  • Using a global keyword as suggested by @nihilok
  • Creating a custom Counter class to handle the integer value (essentially a fancy int).
  • using an int type in the super class and having its methods directly reference it using the super class's name instead of simply using the cls passed in during a class method.

My favourite (least amount of extra parts and most canonical to how I tend to write my objects) was the last method. Where the above translates to something like:

lass Abstract(ABC):
    counter = 0
    @staticmethod
    def increment():
        Abstract.counter += 1. # instead of cls.counter += 1

class A(Abstract):
    pass
class B(Abstract):
    pass

a = A()
b = B()
a.increment()
b.increment()
a.counter == b.counter # now true. 
Alesi Rowland
  • 379
  • 2
  • 16
  • nice, although doesn't do what you set out in your question which is count on instantiations – nihilok Oct 27 '20 at 13:09
  • True - sorry I was writing this up at work and just wrote up the main crux of the issue as was in a rush. to have it increment I believe you would just add __new__ to abstract and call the static method within that. – Alesi Rowland Oct 28 '20 at 10:24