2

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
1
  • 1
    So, in short counter: ClassVar[int] = 0 in your class body, and def __post_init__(self): geeks.counter += 1 as a method. Commented Jan 4, 2023 at 17:19

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.