0

Tell me what is wrong here ?

class GetCounter:

    @classmethod
    def add_counter(cls, name):
        setattr(cls, name, GetCounter(name))

    @classmethod
    def __getattr__(cls, name):
        return getattr(cls, name)

    # obj wise
    def __init__(self, name):
        self.name = name
        self.counter = 0
        self.timestamp = datetime.now()


GetCounter.add_counter('account_info')
counter = GetCounter['account_info']

I get

counter = GetCounter['account_info']
TypeError: 'type' object is not subscriptable

# this works
GetCounter.account_info.counter

I am trying to have a one class that will manage all my counters with a dict like access (dynamic naming).

Solved: How to write a static python getitem method?

thanks.

Si si
  • 317
  • 1
  • 6

0 Answers0