How to use dataclass 'kw_only' correctly?

Viewed 27

I want to have deafult values for Base class variables while not having default values for derived class variables. I have read about 'kw_only' at:

https://medium.com/@aniscampos/python-dataclass-inheritance-finally-686eaf60fbb5

And tried it on my code:

from dataclasses import dataclass, field

@dataclass(kw_only=True)
class Base:
    c:int = field(default=8, compare=False)
    def printBVars(self):
        print("Base:", self.c)

@dataclass(kw_only=True)
class Derive(Base):
    cc:int = field()
    def printDVars(self):
        super().printBVars()
        print("Derive:", self.cc)


a = Derive()
a.printDVars()

But Python is givving this error:

File "<string>", line 8, in <module> TypeError: dataclass() got an unexpected keyword argument 'kw_only'

What am I doing wrong? Im using python 3.9 I have read about some solution suggestions from google, tried some but did not help. such as running: pip install attrs --upgrade

0 Answers
Related