1

what is the best way to achieve class counter implementation with dataclass

class geeks:
    
    # this is used to print the number
    # of instances of a class
    counter = 0
  
    # constructor of geeks class
    def __init__(self,name):

        self.name = name

        # increment
        geeks.counter += 1

with dataclass:

@dataclass
class geeks:
    name : str
Sylvain Page
  • 581
  • 1
  • 7
  • 21
  • 1
    So, in short `counter: ClassVar[int] = 0` in your class body, and `def __post_init__(self): geeks.counter += 1` as a method. – Axe319 Jan 04 '23 at 17:19

0 Answers0